Python HTTP庫 requests 的簡單使用詳情
requests庫實現(xiàn)了HTTP協(xié)議中絕大部分功能,提供了Keep-Alive、連接池、Cookie持久化、HTTP(S)代理支持、連接超時等很多功能特性,最重要的是它同時支持Python2和ython3,而且能在PyPy下完美運行。
使用前需要使用pip install requests命令進行安裝。
1、簡單使用
res = requests.get("http://httpbin.org/get")
# 狀態(tài)碼
print(res.status_code)
# 響應(yīng)頭
print(res.headers["Content-Type"], res.headers["Server"])
# 響應(yīng)內(nèi)容
print(res.text)
執(zhí)行結(jié)果如下:
200
application/json gunicorn/19.9.0
{
"args": {},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
.......
},
"origin": "xxx.xxx.xx.xx",
"url": http://httpbin.org/get
}
另外,http請求還有很多類型,比如POST、PUT、DELETE、HEAD、OPTIONS。requests也都可以以簡單的方式實現(xiàn)。
res = requests.post("http://httpbin.org/post")
res = requests.put("http://httpbin.org/put")
res = requests.delete("http://httpbin.org/delete")
res = requests.head("http://httpbin.org/get")
res = requests.options("http://httpbin.org/get")
由此看來,使用requests庫確實簡單方便。
2、構(gòu)建請求查詢參數(shù)
很多請求都需要在URL中傳遞參數(shù),我們可以用字典來構(gòu)建查詢參數(shù),使用params參數(shù)在URL中添加參數(shù)。
payload = {"wd": "test"}
res = requests.get("https://www.baidu.com/", params=payload)
print(res.url)
運行結(jié)果如下:
https://www.baidu.com/?wd=test
3、構(gòu)建請求頭Headers
requests可以在請求中很簡單的指定請求頭的Headers信息,直接傳遞一個字典給參數(shù)headers即可。
headers = {"user-agent": "Mozilla/5.0", "cookies": "xxx"}
res = requests.get("https://www.baidu.com/", headers=headers)
4、構(gòu)建POST請求數(shù)據(jù)
requests可以非常方便的構(gòu)建POST請求需要的數(shù)據(jù)。如果服務(wù)端接收的的數(shù)據(jù)是表單數(shù)據(jù),可以使用參數(shù)data上送,如果接收的是json格式的數(shù)據(jù),則可以使用json參數(shù)上送。
4.1 表單數(shù)據(jù)
import requests
data = {"key1": "value1", "key2": "value2"}
res = requests.post("http://httpbin.org/post", data=data)
print(res.text)
運行結(jié)果如下:
{
"args": {},
"data": "",
"files": {},
"form": {
"key1": "value1",
"key2": "value2"
},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Content-Length": "23",
"Content-Type": "application/x-www-form-urlencoded",
"Host": "httpbin.org",
"User-Agent": "python-requests/2.26.0",
"X-Amzn-Trace-Id": "Root=1-614d7d91-559333ee19237f845026ef37"
},
"json": null,
"origin": "xxx.xxx.xx.xx",
"url": "http://httpbin.org/post"
}
4.2 json數(shù)據(jù)
import json
import requests
url = "http://httpbin.org/post"
data = {"key": "value"}
data = json.dumps(data)
res = requests.post(url, data=data)
print(res.text)
運行結(jié)果如下:
{
"args": {},
"data": "{\"key\": \"value\"}",
"files": {},
"form": {},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Content-Length": "16",
"Host": "httpbin.org",
"User-Agent": "python-requests/2.26.0",
"X-Amzn-Trace-Id": "Root=1-614d7e91-065887f925dce94d6d03b2e4"
},
"json": {
"key": "value"
},
"origin": "xxx.xxx.xx.xx",
"url": "http://httpbin.org/post"
}
5、獲取響應(yīng)內(nèi)容
使用requests請求處理響應(yīng)體也非常方便靈活,可以使用的屬性有content、text、json() 。
content屬性獲取的是byte類型的數(shù)據(jù)。
import requests
res = requests.get("http://httpbin.org/get")
print(res.content)
text屬性獲取的是str類型的數(shù)據(jù)。
import requests
res = requests.get("http://httpbin.org/get")
print(res.text)
如果返回的內(nèi)容是json格式的數(shù)據(jù)時,就可以使用json()方法返回一個經(jīng)過json.loads()處理后的對象。
import requests url = "http://httpbin.org/post" res = requests.post(url) print(res.json())
6、Cookies
如果響應(yīng)中包含了cookie信息,我們可以使用cookies屬性獲取。
res = requests.get("http://httpbin.org/get")
print(res.cookies)
另外還可以使用cookies參數(shù)向服務(wù)端發(fā)送cookies信息。
url = "http://httpbin.org/cookies"
cookies = {"cookies": "xxxxx"}
r = requests.get(url, cookies=cookies)
print(r.text)
7、超時配置
可以利用timeout參數(shù)來配置最大請求時間。
requests.get("https://baidu.com", timeout=0.01)
8、代理
如果需要使用代理,我們可以通過proxies參數(shù)來配置。
import requests
proxies = {
'http': 'http://175.7.199.202:3256',
'https': 'http://175.7.199.59:3256',
}
requests.get('http://httpbin.org/get', proxies=proxies)
總結(jié):
到此這篇關(guān)于Python HTTP庫 requests 的簡單使用詳情的文章就介紹到這了,更多相關(guān)Python HTTP庫 requests 的簡單使用內(nèi)容請搜索本站以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持本站!
版權(quán)聲明:本站文章來源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非maisonbaluchon.cn所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請聯(lián)系alex-e#qq.com處理。
關(guān)注官方微信