为什么我用Python代码访问SteamGifts时,SteamGifts会返回403状态码?
下面是Python代码
import httpx
url = "https://www.steamgifts.com/"
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:147.0) Gecko/20100101 Firefox/147.0",
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
"Accept-Language": "zh-CN,zh;q=0.9,zh-TW;q=0.8,zh-HK;q=0.7,en-US;q=0.6,en;q=0.5",
"Accept-Encoding": "gzip, deflate, br, zstd",
"DNT": "1",
"Sec-GPC": "1",
"Connection": "keep-alive",
"Upgrade-Insecure-Requests": "1",
"Sec-Fetch-Dest": "document",
"Sec-Fetch-Mode": "navigate",
"Host": "www.steamgifts.com",
"Sec-Fetch-Site": "none",
"Priority": "u=0, i",
"Sec-Fetch-User": "?1",
"TE": "trailers",
}
client = httpx.Client(http2=True, cookies=cookie_io.load())
response = client.get(url, headers=headers)
print(f"Responnse code: {response.status_code}")
我把来自Firefox浏览器的请求头和Cookies复制到了我的Python代码中,并使用HTTP/2。
为什么SteamGifts会返回403响应?
解决方案
这个问题最有可能的原因是TLS指纹识别。你的 httpx 客户端的指纹信息与Firefox不同。Cloudflare通常会检查你的细节,如加密套件、扩展的顺序、椭圆曲线等,并且可以判断这并非真实的浏览器。
你可以使用一个名为 curl_cffi 的库来解决这个问题,从而返回200。
from curl_cffi import requests
response = requests.get(
"https://www.steamgifts.com/",
impersonate="firefox",
cookies={} # or pass your actual cookies here
)
print(response.status_code)
站内所有文章版权归属LeftHeroAI导航站,无授权禁止任何主体转载、抄袭、复制内容,亦不得私自架设镜像站点。一经侵权,本站将通过法律途径追责。