一、問題分析
【疑惑】:使用python的requests庫發(fā)起get或post請求返回403代碼錯誤,使用postman發(fā)起請求發(fā)現(xiàn)狀態(tài)碼<200>竟然成功了。這是什么原因?首先排除ip問題,ip有問題的話postman也訪問不了。難道是headers出現(xiàn)了問題嗎,通過對比發(fā)現(xiàn)也不是headers的問題。那就奇了怪了?文章來源:http://www.zghlxwxcb.cn/news/detail-606665.html
【解疑】:其實遇到這種情況大概率是遇到了“原生模擬瀏覽器 TLS/JA3 指紋的驗證”,瀏覽器和postman都有自帶指紋驗證,而唯獨requests庫沒有。這就讓反爬有了區(qū)分人為和爬蟲的突破口。文章來源地址http://www.zghlxwxcb.cn/news/detail-606665.html
二、問題解決
1、使用 pyhttpx 庫(推薦)
1.1、安裝
pip install pyhttpx
1.2、代碼示例
import pyhttpx
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36",
}
session = pyhttpx.HttpSession()
res = session.get(url='https://www.baidu.com/',headers=headers)
print(res.text)
2、使用 curl_cffi 庫(用得少)
2.1、安裝
pip install curl_cffi
2.2、代碼示例
from curl_cffi import requests
res = requests.get(url='https://www.baidu.com/',impersonate="chrome101")
print(res.text)
3、使用httpx庫(極力推薦)
3.1、安裝
pip install httpx
3.2、代碼示例
import httpx
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36",
}
res = httpx.get(url='https://www.baidu.com/', headers=headers, timeout=10, verify=False)
print(res.text)
到了這里,關于python requests爬蟲返回403錯誤?加了所有特征的請求頭+代理也解決不了的問題處理的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網!