這可能是最快的python http請求庫(類似於requests)
fast_requests
2020年4月29日16:16:03更新:
介紹
用c++寫的python庫,真多執行緒多併發,效能超強,多執行緒下載二進位制的檔案效果最佳
這可能是最快的http請求庫,暫支援get方法,執行緒多的話寬頻可以跑滿。
專案地址:https://github.com/daimiaopeng/fast_requests
測試: 迴圈get("https://baidu.com") 100 次
1.自己寫的 3.4 秒
2.requests (啟用session)10.5 秒
3.grequests 31.3 秒
迴圈get("http://www.nbzhuti.cn/") 1000次
fast_requests 20執行緒 100M寬頻 6.5秒
要求
windows平臺和python3.7.2(其它版本未知)
使用
pip
pip install requests-cpp
手動下載
下載fast_requetst_cpp.pyd
、fast_requests.py
、libcurl.dll
、zlibd1.dll
四個檔案,在專案中匯入fast_requests.py
下載連結:https://2020.lanzous.com/ic2k26h
api
#timeOut 單位為毫秒
get(urls, headers={}, nThread=5, isDebug=False, timeOut=3000, verifySsl=True)
sessionGet(urls, headers={}, nThread=5, isDebug=False, timeOut=3000, verifySsl=True)
#sessionGet和get的區別:sessionGet為每個執行緒持有一個session物件相當於requests的session,建議使用sessionGet速度更快
urls 請求的網址列表
headers 請求頭
nThread 執行緒數(看自己裝置配置,一般設20就差不多了)
isDebug 開啟後會列印每個執行緒每個請求資訊
timeOut 超時
verifySsl ssl驗證
返回 Response 物件列表
Response 物件
Response.text 正文text文字
Response.status_code 狀態碼
Response.header
Response.content() 二進位制資料
Response.Url url
Response.elapsed
Response.cookies
Response.error 暫且用不了
示例程式碼:
import fast_requests
urls = []
headers = {
"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36"
}
for i in range(1, 200):
urls.append("https://www.testyuming.com/")
for i in fast_requests.get(urls, headers, 20):
print(i.text)
for i in fast_requests.sessionGet(urls, headers, 20):
print(i.text)