新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
requests模塊使用-創(chuàng)新互聯(lián)
安裝 requests
# 在命令行工具中使用
pip install requests
# 使用前需要導入
import requests
下面我們來看一下requests庫的幾種常用高級用法
會話對象
- 會話對象可以跨請求保持某些參數(shù),在同一個 Session 實例發(fā)出的所有請求之間保持 cookie,會話也可用來為請求方法提供缺省的數(shù)據(jù);
from requests import Session
s = Session()
# 會話拿到
r = s.get('http://www.baidu.com')
print(r) #
print(r.cookies) # ]>
# 再次發(fā)起請求
r = s.get('https://www.csdn.net/')
print(r.text)
我們可以拿到一個名為
r
的 Response 對象,然后在這個對象中獲取所有我們想要的信息,大家可以自己執(zhí)行代碼看看輸出結(jié)果。
- 會話還可以作為上下文管理器,這樣就能確保 with 區(qū)塊退出后即使發(fā)生了異常會話能被關(guān)閉;
import requests
with requests.get('http://httpbin.org/get', stream=True) as r:
print(r.text)
- 任何時候進行了類似
requests.get()
的調(diào)用,都在做兩件主要的事情,一個是在構(gòu)建一個 Request 對象, 該對象將被發(fā)送到某個服務器請求或查詢一些資源,其二是一旦 requests 得到一個從服務器返回的響應就會產(chǎn)生一個 Response 對象。該響應對象包含服務器返回的所有信息,也包含你原來創(chuàng)建的 Request 對象;
使用Prepared Request對象
- 當你從 API 或者session會話調(diào)用中收到一個 Response 對象時,request 屬性其實是使用了
Prepared Request
對象。
from requests import Request, Session
s = Session()
# 獲取Prepared Request對象
req = Request('GET', url,
data=data,
headers=header
)
prepped = req.prepare()
# do something with prepped.body
# do something with prepped.headers
resp = s.send(prepped,
stream=stream,
verify=verify,
proxies=proxies,
cert=cert,
timeout=timeout
)
print(resp.status_code)
SSL 證書驗證
- Requests 可以為 HTTPS 請求驗證 SSL 證書,就像 web 瀏覽器一樣。verify 默認參數(shù)就是true,開啟SSL 驗證,如果將 verify 設(shè)置為 False,則會忽略對 SSL 證書的驗證,但是會有警告,需要使用
urllib3.disable_warnings()
;
import requests
import urllib3
urllib3.disable_warnings()
# verify默認參數(shù)就是true
ret = requests.get('https://github.com', verify=True)
print(ret.status_code)
# 也可以關(guān)閉verify, 但是會有警告
ret = requests.get('https://github.com', verify=False)
print(ret.status_code)
流式上傳
- Requests支持流式上傳,允許發(fā)送大的數(shù)據(jù)流或文件而無需先把它們讀入內(nèi)存,要使用流式上傳,需要為請求體提供一個類文件對象;
with open('massive-body', 'rb') as f:
requests.post('http://some.url/streamed', data=f)
與requests.post("http://httpbin.org/post", files=files)不一樣
塊編碼請求
- 當下載大文件時,用
Response.iter_content
或許更方便些。requests.get(url)
默認是下載在內(nèi)存中的,下載完成才存到硬盤上,可以用Response.iter_content
來邊下載邊存
import requests
url = "/tupian/20230522/1.mp4"
# 這里設(shè)置stream=true的目的在于不會將內(nèi)容全部儲存在內(nèi)存中,而是根據(jù)chunk_size的大小,去下載內(nèi)容
ret = requests.get(url, stream=True)
with open('1.mp4', 'wb') as f:
for block in ret.iter_content(chunk_size=1024):
f.write(block)
POST 多個分塊編碼的文件
- 使用POST表單提交的方式可以在一個請求中發(fā)送多個文件,只需要把文件設(shè)到一個元組的列表中。
例如,假設(shè)你要上傳多個圖像文件到一個 HTML 表單,使用一個多文件 field 叫做 “images”:
然后把文件設(shè)到一個元組的列表中,其中元組結(jié)構(gòu)為 (form_field_name, file_info):
import requests
url = 'http://httpbin.org/post'
multiple_files = [
# images是input的name屬性
('images', ('foo.png', open('foo.png', 'rb'), 'image/png')),
('images', ('bar.png', open('bar.png', 'rb'), 'image/png'))]
r = requests.post(url, files=multiple_files)
print(r.text)
設(shè)置Proxy代理
- 如果需要使用代理,可以通過為任意請求方法提供 proxies 參數(shù)來往請求中設(shè)置代理,還可以傳遞用戶名和密碼;
import requests
#普通代理
proxies = {
"http": "http://23.10.1.10:80",
"https": "https://23.10.1.11:80",
}
# 往請求中設(shè)置代理(proxies)
r = requests.get("https://www.baidu.com", proxies=proxies)
print(r.status_code)
# 帶有用戶名和密碼的代理, basic認證
proxies = {
"http": "http://user:password@227.1.0.1:9999/",
}
r = requests.get("https://www.baidu.com", proxies=proxies)
print(r.status_code)
# 設(shè)置socks代理
proxies = {
'http': 'socks5://127.0.0.1:1080',
'https': 'socks5://127.0.0.1:1080'
}
r = requests.get("https://www.facebook.com", proxies=proxies)
print(r.status_code)
參考: /tupian/20230522/login.html
分享文章:requests模塊使用-創(chuàng)新互聯(lián)
網(wǎng)頁網(wǎng)址:http://www.dlmjj.cn/article/jcpid.html