爬取CS2皮肤价格API遇403错误,网站可访问求解决
解决Pricempire API 403错误的方案
1. 修正URL中的转义字符
你的URL里错误地将&转义成了&,这会导致API无法正确解析请求参数,直接替换为&即可:
url = 'https://api.pricempire.com/v2/search/items?page=1&priceMin=0&priceMax=1000000&orderBy=price_desc&priceProvider=gamerpay&q=ursus&attr1=undefined&attr2=undefined&appId=730'
2. 优化请求头,更新有效Cookie
- 移除
method、authority这类requests会自动生成的冗余字段; - 补充浏览器原生请求会携带的
Accept、Accept-Language、Referer字段; - Cookie中的
cf_clearance是Cloudflare的临时验证凭证,过期后会失效,必须从你当前浏览器的开发者工具中复制最新的Cookie内容替换:
headers = { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36", "Accept": "application/json, text/plain, */*", "Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8", "Referer": "https://pricempire.com/search?q=ursus", "Cookie": "替换为你当前浏览器中该请求的最新Cookie内容" }
3. 绕过Cloudflare高级验证(若上述方法无效)
如果更新Cookie后仍返回403,说明网站启用了Cloudflare的反爬验证,直接用requests无法绕过,可使用cloudscraper库模拟浏览器完成验证:
- 先安装依赖:
pip install cloudscraper
- 修改代码:
import cloudscraper scraper = cloudscraper.create_scraper() url = 'https://api.pricempire.com/v2/search/items?page=1&priceMin=0&priceMax=1000000&orderBy=price_desc&priceProvider=gamerpay&q=ursus&attr1=undefined&attr2=undefined&appId=730' headers = { "Referer": "https://pricempire.com/search?q=ursus", "Accept": "application/json, text/plain, */*" } r = scraper.get(url, headers=headers) print(r.status_code) print(r.json()) # API返回JSON格式数据,无需用BeautifulSoup解析
4. 注意事项
- 控制请求频率,避免频繁请求导致IP被封禁;
cf_clearance与你的IP、User-Agent绑定,更换设备或IP后需重新获取;- 该接口返回JSON数据,直接用
r.json()解析即可,无需使用BeautifulSoup处理。
内容的提问来源于stack exchange,提问作者doomdaam
相关产品推荐
相关产品推荐

