python3模块: requests
Python標(biāo)準(zhǔn)庫(kù)中提供了:urllib等模塊以供Http請(qǐng)求,但是,它的 API 太渣了。它是為另一個(gè)時(shí)代、另一個(gè)互聯(lián)網(wǎng)所創(chuàng)建的。它需要巨量的工作,甚至包括各種方法覆蓋,來(lái)完成最簡(jiǎn)單的任務(wù)。
發(fā)送GET請(qǐng)求
import urllib.requestf = urllib.request.urlopen('http://www.webxml.com.cn//webservices/qqOnlineWebService.asmx/qqCheckOnline?qqCode=424662508') result = f.read().decode('utf-8')發(fā)送攜帶請(qǐng)求頭的GET請(qǐng)求
import urllib.requestreq = urllib.request.Request('http://www.example.com/') req.add_header('Referer', 'http://www.python.org/') r = urllib.request.urlopen(req)result = f.read().decode('utf-8')更多內(nèi)容點(diǎn)擊查看官方文檔
Requests 是使用 Apache2 Licensed 許可證的 基于Python開(kāi)發(fā)的HTTP 庫(kù),其在Python內(nèi)置模塊的基礎(chǔ)上進(jìn)行了高度的封裝,從而使得Pythoner進(jìn)行網(wǎng)絡(luò)請(qǐng)求時(shí),變得美好了許多,使用Requests可以輕而易舉的完成瀏覽器可有的任何操作。
GET請(qǐng)求
# 1、無(wú)參數(shù)實(shí)例import requestsret = requests.get('https://github.com/timeline.json')print(ret.url) print(ret.text)# 2、有參數(shù)實(shí)例import requestspayload = {'key1': 'value1', 'key2': 'value2'} ret = requests.get("http://httpbin.org/get", params=payload)print(ret.url) print(ret.text)POST請(qǐng)求
# 1、基本POST實(shí)例import requestspayload = {'key1': 'value1', 'key2': 'value2'} ret = requests.post("http://httpbin.org/post", data=payload)print(ret.text)# 2、發(fā)送請(qǐng)求頭和數(shù)據(jù)實(shí)例import requests import jsonurl = 'https://api.github.com/some/endpoint' payload = {'some': 'data'} headers = {'content-type': 'application/json'}ret = requests.post(url, data=json.dumps(payload), headers=headers)print(ret.text) print(ret.cookies)其他請(qǐng)求
requests.get(url, params=None, **kwargs) requests.post(url, data=None, json=None, **kwargs) requests.put(url, data=None, **kwargs) requests.head(url, **kwargs) requests.delete(url, **kwargs) requests.patch(url, data=None, **kwargs) requests.options(url, **kwargs)# 以上方法均是在此方法的基礎(chǔ)上構(gòu)建 requests.request(method, url, **kwargs)更多requests模塊相關(guān)文檔點(diǎn)擊查看
實(shí)例:檢測(cè)QQ賬號(hào)是否在線
import urllib import requests from xml.etree import ElementTree as ET# 使用內(nèi)置模塊urllib發(fā)送HTTP請(qǐng)求,或者XML格式內(nèi)容 """ f = urllib.request.urlopen('http://www.webxml.com.cn//webservices/qqOnlineWebService.asmx/qqCheckOnline?qqCode=424662508') result = f.read().decode('utf-8') """# 使用第三方模塊requests發(fā)送HTTP請(qǐng)求,或者XML格式內(nèi)容 r = requests.get('http://www.webxml.com.cn//webservices/qqOnlineWebService.asmx/qqCheckOnline?qqCode=424662508') result = r.text# 解析XML格式內(nèi)容 node = ET.XML(result)# 獲取內(nèi)容 if node.text == "Y":print("在線") else:print("離線")實(shí)例:查看火車(chē)停靠信息
import urllib import requests from xml.etree import ElementTree as ET# 使用內(nèi)置模塊urllib發(fā)送HTTP請(qǐng)求,或者XML格式內(nèi)容 """ f = urllib.request.urlopen('http://www.webxml.com.cn/WebServices/TrainTimeWebService.asmx/getDetailInfoByTrainCode?TrainCode=G666&UserID=') result = f.read().decode('utf-8') """# 使用第三方模塊requests發(fā)送HTTP請(qǐng)求,或者XML格式內(nèi)容 r = requests.get('http://www.webxml.com.cn/WebServices/TrainTimeWebService.asmx/getDetailInfoByTrainCode?TrainCode=G666&UserID=') result = r.text# 解析XML格式內(nèi)容 root = ET.XML(result) for node in root.iter('TrainDetailInfo'):print(node.find('TrainStation').text,node.find('StartTime').text,node.tag,node.attrib)轉(zhuǎn)載于:https://www.cnblogs.com/guigujun/p/7614506.html
總結(jié)
以上是生活随笔為你收集整理的python3模块: requests的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: [转]Android中handler机制
- 下一篇: 《Java程序设计》第2周学习总结