Scrapy爬虫本地运行正常,部署至Zyte后出现403错误求助
Scrapy爬虫部署Zyte平台后出现403错误问题
问题描述
Scrapy爬虫逻辑为读取待爬取链接,最终发送POST请求并解析数据。本地环境可正常采集数据,但部署至Zyte平台后出现403错误。
核心请求代码
yield scrapy.Request( url=STORE_URL.format(zip_code), headers=headers_1, meta={"item_id": item_id, "zip_code": zip_code}, dont_filter=True, callback=self.parse_a )
yield scrapy.Request( url=API_URL, method="POST", headers=headers, body=json.dumps(payload(item_id,zip_code, store_id)), meta={"prod_code": item_id, "zip_code": zip_code}, dont_filter=True, callback=self.parse )
配置的User-Agent
USER_AGENT = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.125 Safari/537.36'
错误日志
14: 2023-06-18 03:10:58 INFO [scrapy.extensions.telnet] Telnet console listening on 0.0.0.0:6023 15: 2023-06-18 03:10:58 INFO [scrapy.spidermiddlewares.httperror] Ignoring response <403 https://www.homedepot.com/StoreSearchServices/v2/storesearch?address=30308&radius=50&pagesize=30>: HTTP status code is not handled or not allowed 16: 2023-06-18 03:11:04 INFO [scrapy.spidermiddlewares.httperror] Ignoring response <403 https://www.homedepot.com/StoreSearchServices/v2/storesearch?address=2125&radius=50&pagesize=30>: HTTP status code is not handled or not allowed 17: 2023-06-18 03:11:11 INFO [scrapy.spidermiddlewares.httperror] Ignoring response <403 https://www.homedepot.com/StoreSearchServices/v2/storesearch?address=60607&radius=50&pagesize=30>: HTTP status code is not handled or not allowed
解决建议
- 对齐本地与Zyte的请求头:检查
headers_1和headers中的字段,确保包含Referer、Accept、Accept-Language等关键内容,完全复刻本地请求的头信息。Zyte平台可能默认修改部分请求头,需手动指定完整配置。 - 更新User-Agent版本:当前使用的Chrome 84版本过于陈旧,替换为最新的Chrome或Firefox官方User-Agent,避免被目标网站识别为过时爬虫客户端。
- 启用Zyte浏览器渲染模式:目标网站(Home Depot)反爬机制严格,API接口可能验证请求环境。开启Zyte智能代理的浏览器渲染功能(配置
zyte_smartproxy=True和render=True),模拟真实浏览器行为绕过检测。 - 控制请求频率:本地请求频率较低,部署后可能因请求过快触发反爬。在
settings.py中设置DOWNLOAD_DELAY = 2,或开启AUTOTHROTTLE_ENABLED = True自动调整请求间隔。 - 校验请求参数完整性:检查
payload生成的参数是否包含目标API要求的动态字段(如时间戳、签名令牌),本地环境可能自动获取这些参数,Zyte环境下需手动处理生成逻辑。 - 添加403重试逻辑:修改爬虫的错误处理配置,将403加入重试列表(
RETRY_HTTP_CODES = [403]),配合随机延迟重试,避免单次请求失败直接放弃。
内容的提问来源于stack exchange,提问作者FalloutATS21
相关产品推荐
相关产品推荐

