第三方requests模塊
阿新 • • 發佈:2017-09-02
inf ons ttr online code res 第三方 requests nbsp
requests模塊
requests:
將python編譯器當做瀏覽器使用,可以訪問網站,並且接受返回的數據(字符串)
1 import requests 2 import json 3 4 # 發送http請求 5 response = requests.get("http://www.weather.com.cn/adat/sk/101010500.html") 6 response.encoding = ‘utf-8‘ 7 result = response.text 8 print(type(result), result) 9 10 # <class ‘str‘> {"weatherinfo":{"city":"懷柔","cityid":"101010500","temp":"9","WD":"南風",11 # "WS":"1級","SD":"29%","WSE":"1","time":"10:25","isRadar":"1","Radar":"JC_RADAR_AZ9010_JB", 12 # "njd":"暫無實況","qy":"1007"}} 13 14 15 dic = json.loads(result) 16 print(type(dic), dic) 17 18 # <class ‘dict‘> {‘weatherinfo‘: {‘city‘: ‘懷柔‘, ‘cityid‘: ‘101010500‘, ‘temp‘: ‘9‘, ‘WD‘: ‘南風‘, 19 # ‘WS‘: ‘1級‘, ‘SD‘: ‘29%‘, ‘WSE‘: ‘1‘, ‘time‘: ‘10:25‘, ‘isRadar‘: ‘1‘, ‘Radar‘: ‘JC_RADAR_AZ9010_JB‘,20 # ‘njd‘: ‘暫無實況‘, ‘qy‘: ‘1007‘}}
1.檢查QQ在線情況
1 import requests 2 3 # 使用第三方模塊requests發送HTTP請求,或者XML格式內容 4 r = requests.get(‘http://www.webxml.com.cn//webservices/qqOnlineWebService.asmx/qqCheckOnline?qqCode=123456789‘) 5 result = r.text #xml的字符串類型 6 7 8 from xml.etree import ElementTree as ET9 10 # 解析XML格式內容 11 node = ET.XML(result) #XML接收一個字符串,格式化為特殊的對象 12 print(node) 13 14 # 獲取內容 15 if node.text == "Y": #特殊的對象可以訪問自己各個節點的信息 16 print("在線") 17 else: 18 print("離線")
2.火車站點詳情
1 import requests 2 3 from xml.etree import ElementTree as ET 4 5 # 使用第三方模塊requests發送HTTP請求,或者XML格式內容 6 r = requests.get( 7 ‘http://www.webxml.com.cn/WebServices/TrainTimeWebService.asmx/getDetailInfoByTrainCode?TrainCode=G666&UserID=‘) 8 result = r.text 9 10 # 解析XML格式內容,把字符串解析成XML 11 root = ET.XML(result) 12 13 print(root.tag) 14 for node in root: 15 print(node) 16 for node_node in node: 17 print(node_node) 18 for node_node_node in node_node: 19 print(node_node_node)
第三方requests模塊