python 对象序列化xml_python3----模块(序列化(json&pickle)+XML+requests)

3、创建XML文档

1 from xml.etree importElementTree as ET2

3

4 #创建根节点

5 root = ET.Element("famliy")6

7

8 #创建节点大儿子

9 son1 = ET.Element('son', {'name': '儿1'})10 #创建小儿子

11 son2 = ET.Element('son', {"name": '儿2'})12

13 #在大儿子中创建两个孙子

14 grandson1 = ET.Element('grandson', {'name': '儿11'})15 grandson2 = ET.Element('grandson', {'name': '儿12'})16 son1.append(grandson1)17 son1.append(grandson2)18

19

20 #把儿子添加到根节点中

21 root.append(son1)22 root.append(son1)23

24 tree =ET.ElementTree(root)25 tree.write('oooo.xml',encoding='utf-8', short_empty_elements=False)26

27 创建方式(一)28

29 创建方式一

由于原生保存的XML时默认无缩进,如果要设置缩进的话,需要修改保存方式:

1 from xml.etree importElementTree as ET2 from xml.dom importminidom3

4

5 defprettify(elem):6 """将节点转换成字符串,并添加缩进。7 """

8 rough_string = ET.tostring(elem, 'utf-8')9 reparsed =minidom.parseString(rough_string)10 return reparsed.toprettyxml(indent="\t")11

12 #创建根节点

13 root = ET.Element("famliy")14

15

16 #创建大儿子

17 #son1 = ET.Element('son', {'name': '儿1'})

18 son1 = root.makeelement('son', {'name': '儿1'})19 #创建小儿子

20 #son2 = ET.Element('son', {"name": '儿2'})

21 son2 = root.makeelement('son', {"name": '儿2'})22

23 #在大儿子中创建两个孙子

24 #grandson1 = ET.Element('grandson', {'name': '儿11'})

25 grandson1 = son1.makeelement('grandson', {'name': '儿11'})26 #grandson2 = ET.Element('grandson', {'name': '儿12'})

27 grandson2 = son1.makeelement('grandson', {'name': '儿12'})28

29 son1.append(grandson1)30 son1.append(grandson2)31

32

33 #把儿子添加到根节点中

34 root.append(son1)35 root.append(son1)36

37

38 raw_str =prettify(root)39

40 f = open("xxxoo.xml",'w',encoding='utf-8')41 f.write(raw_str)42 f.close()43

44 定义缩进函数

4、命名空间

详细介绍,猛击这里

1 from xml.etree importElementTree as ET2

3 ET.register_namespace('com',"http://www.company.com") #some name

4

5 #build a tree structure

6 root = ET.Element("{http://www.company.com}STUFF")7 body = ET.SubElement(root, "{http://www.company.com}MORE_STUFF", attrib={"{http://www.company.com}hhh": "123"})8 body.text = "STUFF EVERYWHERE!"

9

10 #wrap it in an ElementTree instance, and save as XML

11 tree =ET.ElementTree(root)12

13 tree.write("page.xml",14 xml_declaration=True,15 encoding='utf-8',16 method="xml")17

18 命名空间

三、requests模块

requests是使用Apache2 Licensed许可证的基于python开发的HTTP库,其在python内置模块的基础上进行了高度的封装,从而使pythoner在进行网络请求时,变得更容易,使用requests可以轻而易举的完成浏览器可有的任何操作。requests模块是第三方开发库里的模块,调用前要先下载安装该模块。

1、安装模块

pip3 install requests

2、使用模块

1 无参数,直接访问网站,拿数据2 importrequests3 response = requests.get("http://www.weather.com.cn/adat/sk/101010500.html")4 response.encoding='utf-8'

5 result =response.text6 print(result)7 ###################执行结果如下:#####################

8 {"weatherinfo":9 {"city":"怀柔",10 "cityid":"101010500",11 "temp":"9",12 "WD":"南风",13 "WS":"1级",14 "SD":"29%",15 "WSE":"1",16 "time":"10:25",17 "isRadar":"1",18 "Radar":"JC_RADAR_AZ9010_JB",19 "njd":"暂无实况","qy":"1007"}20 }

1 有参数,参数用params=变量名传入,结果中会先将参数打印在头部2 importrequests3 payload={'k1':'value1','k2':'value2'}4 ret=requests.get("http://www.weather.com.cn/adat/sk/101010500.html",params=payload)5 ret.encoding='utf-8'

6 print(ret.url)7 print(ret.text)8 ###############执行结果如下:################

9 http://www.weather.com.cn/adat/sk/101010500.html?k1=value1&k2=value210 {"weatherinfo":11 {"city":"怀柔",12 "cityid":"101010500",13 "temp":"9",14 "WD":"南风",15 "WS":"级",16 "SD":"29%",17 "WSE":"1",18 "time":"10:25",19 "isRadar":"1",20 "Radar":"JC_RADAR_AZ9010_JB",21 "njd":"暂无实况",22 "qy":"1007"}23 }

1 #1、基本POST实例

2

3 importrequests4

5 payload = {'key1': 'value1', 'key2': 'value2'}6 ret = requests.post("http://httpbin.org/post", data=payload)7

8 print(ret.text)9

10

11 #2、发送请求头和数据实例

12

13 importrequests14 importjson15

16 url = 'https://api.github.com/some/endpoint'

17 payload = {'some': 'data'}18 headers = {'content-type': 'application/json'}19

20 ret = requests.post(url, data=json.dumps(payload), headers=headers)21

22 print(ret.text)23 print(ret.cookies)#cookies验证,比如登录后服务器会发送一串XX码保存在客户端内存中或硬盘中,即为cookies

24

25 POST请求

更多requests模块相关的文档见:http://cn.python-requests.org/zh_CN/latest/

3、用requests模块发送HTTP请求,请求结束后解析XML的实例

1 importurllib2 importrequests3 from xml.etree importElementTree as ET4

5 #使用内置模块urllib发送HTTP请求,或者XML格式内容

6 """

7 f = urllib.request.urlopen('http://www.webxml.com.cn//webservices/qqOnlineWebService.asmx/qqCheckOnline?qqCode=424662508')8 result = f.read().decode('utf-8')9 """

10 @@显然使用内置urllib模块的方式比较繁琐,所以请选择忘记,并选择用下面的requests模块来发送HTTP请求。@@11

12 #使用第三方模块requests发送HTTP请求,或者XML格式内容

13 r = requests.get('http://www.webxml.com.cn//webservices/qqOnlineWebService.asmx/qqCheckOnline?qqCode=424662508')14 result =r.text15

16 #解析XML格式内容

17 node = ET.XML(result)#这里的ET.XML在此篇二、XML模块中有详解

18

19 #获取内容

20 if node.text == "Y":21 print("在线")22 else:23 print("离线")24

25 eg1:检测QQ账号是否在线

1 importurllib2 importrequests3 from xml.etree importElementTree as ET4

5 #使用内置模块urllib发送HTTP请求,或者XML格式内容

6 """

7 f = urllib.request.urlopen('http://www.webxml.com.cn/WebServices/TrainTimeWebService.asmx/getDetailInfoByTrainCode?TrainCode=G666&UserID=')8 result = f.read().decode('utf-8')9 """

10

11 #使用第三方模块requests发送HTTP请求,或者XML格式内容

12 r = requests.get('http://www.webxml.com.cn/WebServices/TrainTimeWebService.asmx/getDetailInfoByTrainCode?TrainCode=G666&UserID=')13 result =r.text14

15 #解析XML格式内容

16 root =ET.XML(result)17 for node in root.iter('TrainDetailInfo'):18 print(node.find('TrainStation').text,node.find('StartTime').text,node.tag,node.attrib)19

20 eg2:查看火车停靠信息

注:更多接口猛击这里

四、总结

1、json数据传输时用的

2、XML一种文件表现形式,形似树

3、requests模块就是程序媛们访问网站或者API接口拿数据时用的,记住requests比urllib好用,然后记住requests.get(),requests.post()就行了

你可能感兴趣的:(python,对象序列化xml)