Wget/Curl请求Target库存接口提示tcin为空的问题求助
解决Target RedSky接口Curl/Wget请求报错"tcin变量为空"的方法
核心原因
Target的RedSky接口会校验请求的**用户代理(User-Agent)**和其他请求头,默认的Curl/Wget请求头会被识别为非浏览器请求,触发反爬校验,导致参数被过滤(即使URL里tcin存在,后端也会判定为空)。
具体解决步骤
1. 模拟浏览器请求头
直接在Curl/Wget中添加浏览器的User-Agent,同时补充必要的Referer头(模拟从Target商品页跳转过来的请求)。
Curl示例命令:
curl -H "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36" \ -H "Referer: https://www.target.com/p/A-81640508" \ "https://redsky.target.com/redsky_aggregations/v1/web_platform/product_fulfillment_v1?key=9f36aeafbe60771e321a7cc95a78140772ab3e96&is_bot=false&tcin=53180239&store_id=2381&zip=12590&state=NY&latitude=41.640&longitude=-73.830&scheduled_delivery_store_id=1856&required_store_id=2381&has_required_store_id=true&channel=WEB&page=%2Fp%2FA-81640508"
Wget示例命令:
wget --user-agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36" \ --header="Referer: https://www.target.com/p/A-81640508" \ "https://redsky.target.com/redsky_aggregations/v1/web_platform/product_fulfillment_v1?key=9f36aeafbe60771e321a7cc95a78140772ab3e96&is_bot=false&tcin=53180239&store_id=2381&zip=12590&state=NY&latitude=41.640&longitude=-73.830&scheduled_delivery_store_id=1856&required_store_id=2381&has_required_store_id=true&channel=WEB&page=%2Fp%2FA-81640508"
2. 封装成定时监控脚本(适配Cron)
可以写一个Shell脚本,定时请求接口,解析JSON判断库存状态,触发告警(比如邮件、推送)。
示例脚本(target_stock_monitor.sh):
#!/bin/bash # 配置参数 URL="https://redsky.target.com/redsky_aggregations/v1/web_platform/product_fulfillment_v1?key=9f36aeafbe60771e321a7cc95a78140772ab3e96&is_bot=false&tcin=53180239&store_id=2381&zip=12590&state=NY&latitude=41.640&longitude=-73.830&scheduled_delivery_store_id=1856&required_store_id=2381&has_required_store_id=true&channel=WEB&page=%2Fp%2FA-81640508" USER_AGENT="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36" REFERER="https://www.target.com/p/A-81640508" ALERT_EMAIL="your_email@example.com" # 请求接口并解析库存状态 RESPONSE=$(curl -s -H "User-Agent: $USER_AGENT" -H "Referer: $REFERER" "$URL") IN_STOCK=$(echo "$RESPONSE" | grep -o '"is_out_of_stock_in_all_store_locations":true' | wc -l) # 判断是否补货 if [ $IN_STOCK -eq 0 ]; then echo "Target商品(TCIN:53180239)已补货!" | mail -s "Target库存告警" $ALERT_EMAIL # 可选:添加其他告警方式,比如pushbullet、telegram机器人 fi
然后给脚本加执行权限:
chmod +x target_stock_monitor.sh
配置Cron定时任务(比如每10分钟检查一次):
crontab -e
添加一行:
*/10 * * * * /path/to/target_stock_monitor.sh
3. 注意事项
- 定期更新User-Agent:如果后续又出现报错,可能是Target更新了反爬规则,换成最新的浏览器User-Agent即可(可以在浏览器开发者工具的Network面板中查看)。
- 避免过于频繁请求:不要把定时间隔设得太短,否则可能被IP封禁,建议间隔5-15分钟。
- 接口参数稳定性:RedSky接口的
key参数可能会变动,如果后续请求失败,需要从Target商品页的Network请求中重新获取最新的key。
内容的提问来源于stack exchange,提问作者aric8456
相关产品推荐
相关产品推荐

