使用Python Requests实现Supreme商品加购失败求助
问题描述
尝试用Python的Requests库实现Supreme网站商品加购功能,发送加购请求后无报错,但返回响应显示加购失败。相关代码及返回结果如下:
代码示例
#imports import requests from bs4 import BeautifulSoup #constants baseurl = "https://www.supremenewyork.com/" product_category = "" size = ["Medium"] product_keywords = ["Supreme®/The North Face® Steep Tech Fleece Pant"] product_style = ["Brown"] #functions def carting(url): session = requests.Session() r = session.get(baseurl+url) soup = BeautifulSoup(r.text, "html.parser") name = soup.find("h1", {"itemprop" : "name"}).text style = soup.find("p", {"itemprop" : "model"}).text for keyword in product_keywords: if keyword in name: for keyword in product_style: if keyword in style: print("Product Found! Adding to cart...") form = soup.find("form", {"id" : "cart-add"}) payload = { "utf8" : "✓", "authenticity_token" : form.find("input", {"name" : "authenticity_token"})["value"], "style" : form.find("input", {"name" : "style"})["value"], "size" : "92001", #form.find("select", {"id" : "size"})["value"] Need to rework getting Size through Keyword "qty" : "1" } headers = { "user-agent" : "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36", "origin" : "https://www.supremenewyork.com", "referer" : baseurl + url, "path" : form["action"], 'Host': 'www.supremenewyork.com', 'Accept': 'application/json', 'Proxy-Connection': 'keep-alive', 'X-Requested-With': 'XMLHttpRequest', 'Accept-Encoding': 'gzip, deflate', 'Accept-Language': 'en-us', 'Content-Type': 'application/x-www-form-urlencoded', 'Origin': 'http://www.supremenewyork.com', 'Connection': 'keep-alive', } response = session.post(baseurl+form["action"], data=payload) print(response.text) return(session) carting("/shop/pants/mj1czv0pa/jcyp91a8w")
返回结果
Product Found! Adding to cart... {"cart":[],"success":false}
怀疑是请求头设置不符合网站要求,不确定需包含哪些必要请求头,请求解决该问题。
解决方案
针对Supreme加购失败的问题,从请求头、会话状态、参数准确性三个方向排查修复:
1. 修复请求头的冲突与冗余问题
你的请求头存在两处关键错误:
- 重复设置了
Origin字段(同时有http和https版本),Supreme站点为HTTPS,需统一为https://www.supremenewyork.com - 多余的
path字段不属于请求头范畴,需要移除 - 精简后的核心请求头如下:
headers = { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36", "Origin": "https://www.supremenewyork.com", "Referer": baseurl + url, "Host": "www.supremenewyork.com", "Accept": "application/json", "X-Requested-With": "XMLHttpRequest", "Accept-Language": "en-US,en;q=0.9", "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8", "Connection": "keep-alive" }
2. 确保会话状态一致性
Supreme依赖Cookie跟踪会话,你已使用requests.Session(),但需确认:
- GET商品页面时保存的Cookie被自动带到POST请求中(Session会自动处理,可打印
session.cookies确认包含_supreme_session等关键字段)
3. 验证Size参数的准确性
硬编码的size: "92001"可能不匹配当前商品的真实尺码ID,建议动态解析页面获取:
# 替换硬编码的size值 size_select = soup.find("select", {"id": "size"}) target_size_id = None for option in size_select.find_all("option"): if size[0] in option.text: target_size_id = option["value"] break # 在payload中使用获取到的target_size_id payload = { "utf8" : "✓", "authenticity_token" : form.find("input", {"name" : "authenticity_token"})["value"], "style" : form.find("input", {"name" : "style"})["value"], "size" : target_size_id, "qty" : "1" }
4. 额外注意事项
- Supreme有反爬机制,频繁请求可能被拦截,建议添加1-2秒的随机延迟
- 确认
authenticity_token是从当前页面form中获取的最新值,避免使用过期token - 检查POST请求URL是否正确:打印
baseurl+form["action"],确认格式为https://www.supremenewyork.com/shop/<product-id>/add
内容的提问来源于stack exchange,提问作者Chick3nwing
相关产品推荐
相关产品推荐

