1安装
软件管理工具
安装pip3
依赖setuptools
pip3 install requests
2源码安装
下载代码,安装
https://codeload.github.com/requests/requests/legacy.tar.gz/master
先下载然后解太进入目录中,执行
python setup.py install
1 import requests 2 response = requests.get("http://www.weather.com.cn/adat/sk/101010500.html") 3 response.encoding = "utf-8" 4 result =response.text 5 print(result)
检查QQ是否在线
1 import requests 2 #发送一个requests http请求,或者XML格式内容 3 r = requests.get("http://www.webxml.com.cn//webservices/qqOnlineWebService.asmx/qqCheckOnline?qqCode=45678966456") 4 result = r.text 5 from xml.etree import ElementTree as ET 6 #解析XML格式内容 7 #XML接收一个参数:字符串,格式化为特殊的对像 8 node = ET.XML(result) 9 #获取内容 10 if node.text == "Y": 11 print("在线") 12 else: 13 print("离线")
火车时刻列表
1 import requests 2 r = requests.get("http://ws.webxml.com.cn/WebServices/TrainTimeWebService.asmx/getDetailInfoByTrainCode?TrainCode=T96&UserID=") 3 result = r.text 4 # print(result) 5 from xml.etree import ElementTree as ET 6 root = ET.XML(result) 7 for node in root.iter("TrainDetailInfo"): 8 print(node.find('TrainStation').text,node.find('ArriveTime').text)
XML操作
初始化:
1 #!/usr/bin/env python3 2 from xml.etree import ElementTree as ET 3 from xml.dom import minidom 4 def prettify(elem): 5 #将节点专成字符串,并添加缩进 6 rough_string =ET.tostring(elem,'utf-8') 7 reparsed = minidom.parseString(rough_string) 8 return reparsed.toprettyxml(indent=' ') 9 #创建 根节点 10 root = ET.Element("famliy") 11 #创建子节点,大儿子和小儿子 12 son1 = ET.Element('son',{'name':'儿1'}) 13 son2 = ET.Element('son',{'name':'儿2'}) 14 #大儿子中创建 两个孙子 15 grandson1 = ET.Element('grandson',{'name':'儿11'}) 16 grandson2 = ET.Element('grandson',{'name':'儿12'}) 17 son1.append(grandson1) 18 son1.append(grandson2) 19 20 #把儿子添加到根节点 21 root.append(son1) 22 root.append(son2) 23 24 raw_str = prettify(root) 25 f = open('family.xml','w',encoding='utf-8') 26 f.write(raw_str) 27 f.close()
增加
1 #!/usr/bin/env python3 2 from xml.etree import ElementTree as ET 3 #直接解析xml文件 4 tree = ET.parse('first.xml') 5 #获取xml文件的根节点,element类型 6 root = tree.getroot() 7 # print(root.tag) 8 # print(root.attrib) 9 # print(root.text) 10 #创建 根节点element类型 11 # son = root.makeelement('tt',{"kk":"vv"}) 12 # #添加儿子节点 13 # root.append(son) 14 # s = son.makeelement("tt",{"kk":"123456"}) 15 #对像==》类创建 16 son = ET.Element("aa",{"kk":"vv"}) 17 root.append(son) 18 s = ET.Element("aa",{"kk":"aa1234"}) 19 son.append(s) 20 21 22 tree.write("out.xml")
修改删除
tag, find,iter,set,get,items
1 from xml.etree import ElementTree as ET 2 root = ET.XML(open('first.xml','r',encoding='utf-8').read()) 3 print(root.tag) 4 for node in root: 5 print(node,type(node)) 6 print(node.tag,node.attrib,node.find('year').text) 7 tree = ET.parse('first.xml') 8 root = tree.getroot() 9 print(root.tag) 10 11 for node in root.iter('year'): 12 new_year = int(node.text)+1#修改text内的容 13 node.text = str(new_year) 14 # node.set('name','alex') 15 # node.set('age','19') 16 del node.attrib['name']#删除name的内容 17 tree.write('first.xml')
configparser主要用一类配置文件的修写创建如
本质就是利用open来操作
[section1]
k1 = v1
k1:v1
[section2]
k2 = v2
1 #!/usr/bin/env python3 2 import configparser 3 conf = configparser.ConfigParser() 4 conf.read("ini",encoding='utf-8') 5 6 # result = conf.sections() 7 #获取节点信息 8 # print(result) 9 # ret = conf.get('wang','age') 10 #获取节点信息内容age的值 11 # print(ret) 12 # 13 # ret1 = conf.items('wang') 14 # print(ret1) 15 # 16 # has_sec = conf.has_section('wang') 17 # print(has_sec) 18 #添加节点 19 conf.add_section("robert1") 20 21 #节点的值 22 conf.set('robert1','age','15') 23 #节点移除 24 # conf.remove_section('robert') 25 #节点中值移去 26 # conf.remove_option('robert','age') 27 #修改设置 28 conf.set('robert','age','45') 29 conf.write(open('ini_o1','w',encoding='utf-8'))