Python HTTP庫 requests 的簡單使用詳情
requests
庫實現(xiàn)了HTTP協(xié)議中絕大部分功能,提供了Keep-Alive
、連接池、Cookie
持久化、HTTP(S)代理支持、連接超時等很多功能特性,最重要的是它同時支持Python2
和ython3,而且能在PyPy下完美運(yùn)行。
使用前需要使用pip install requests
命令進(jìn)行安裝。
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)
運(yùn)行結(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)
運(yùn)行結(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": " }
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)
運(yùn)行結(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)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python實現(xiàn)分析apache和nginx日志文件并輸出訪客ip列表的方法
這篇文章主要介紹了python實現(xiàn)分析apache和nginx日志文件并輸出訪客ip列表的方法,涉及Python操作日志文件的技巧,非常具有實用價值,需要的朋友可以參考下2015-04-04Pycharm插件(Grep Console)自定義規(guī)則輸出顏色日志的方法
這篇文章主要介紹了Pycharm插件(Grep Console)自定義規(guī)則輸出顏色日志的方法,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-05-05Python Django 添加首頁尾頁上一頁下一頁代碼實例
這篇文章主要介紹了Python Django 添加首頁尾頁上一頁下一頁代碼實例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-08-08利用 Python 實現(xiàn)多任務(wù)進(jìn)程
這篇文章主要介紹如何利用 Python 實現(xiàn)多任務(wù)進(jìn)程,正在執(zhí)行的程序,由程序、數(shù)據(jù)和進(jìn)程控制塊組成,是正在執(zhí)行的程序,程序的一次執(zhí)行過程,是資源調(diào)度的基本單位。下面就來詳細(xì)介紹改內(nèi)容,需要的朋友可以參考一下2021-10-10Python使用gluon/mxnet模塊實現(xiàn)的mnist手寫數(shù)字識別功能完整示例
這篇文章主要介紹了Python使用gluon/mxnet模塊實現(xiàn)的mnist手寫數(shù)字識別功能,結(jié)合完整實例形式分析了Python調(diào)用gluon/mxnet模塊識別手寫字的具體實現(xiàn)技巧,需要的朋友可以參考下2019-12-12Python 實現(xiàn)使用空值進(jìn)行賦值 None
這篇文章主要介紹了Python 實現(xiàn)使用空值進(jìn)行賦值 None,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03