Shopify Python下单脚本运费报错:无效运费率问题排查
问题
我正在编写Python版Shopify购买机器人脚本,使用以下JSON payload:
{ 'utf8': '✓', '_method': 'patch', 'authenticity_token': '', 'previous_step': 'payment_method', 'step': '', 's': 'east-66ff824e354621d8fcedf11a05967ac6', 'checkout[payment_gateway]': '83961729', 'checkout[credit_card][vault]': 'false', 'checkout[different_billing_address]': 'true', 'checkout[billing_address][first_name]': 'xxx', 'checkout[billing_address][last_name]': 'xxx', 'checkout[billing_address][address1]': 'xxx', 'checkout[billing_address][address2]': '', 'checkout[billing_address][city]': 'xxx', 'checkout[billing_address][country]': 'xxx', 'checkout[billing_address][province]': 'xxx', 'checkout[billing_address][zip]': 'xxx', 'checkout[billing_address][phone]': 'xxx', 'checkout[shipping_rate][id]': 'shopify-UPS®%20Ground-12.03', 'checkout[shipping_rate][price]': '12.03', 'checkout[shipping_rate][title]': 'UPS®%20Ground', 'complete': '1', 'checkout[client_details][browser_width]': '1307', 'checkout[client_details][browser_height]': '1690', 'checkout[client_details][javascript_enabled]': '1', 'g-recaptcha-repsonse': '', 'button': '' }
以及以下Python代码:
import requests import urllib3 session = requests.session() urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) r = add_to_cart(session, variant) cj = r.cookies link = "https://store.merchant.com/14391668/checkouts/" r = session.post(link, cookies=cj, data=payload, verify=True)
执行后从r.text中始终收到报错:Your cart has been updated and the previous shipping rate isn’t valid. Select a new rate.
请问我缺少哪些步骤才能成功完成结账?
解决方案
动态获取有效配送费率
Shopify的配送费率会根据购物车商品、收货地址动态生成,硬编码的shopify-UPS®%20Ground-12.03大概率已失效。正确操作:- 添加商品到购物车后,GET请求结账页面,解析页面内的配送选项,提取当前有效的
id、price、title值。 - 将这些动态获取的值替换payload中
checkout[shipping_rate]下的对应字段。
- 添加商品到购物车后,GET请求结账页面,解析页面内的配送选项,提取当前有效的
遵循分步结账流程
不能直接跳转到支付步骤,需按Shopify的结账顺序执行:- 先提交收货地址,设置
step='shipping_address',获取会话更新和可用配送选项。 - 选择配送费率后,提交到
step='shipping_method',确认配送方式有效性。 - 最后进入支付步骤,设置
step='payment_method'提交支付信息。
- 先提交收货地址,设置
完善会话与请求头
- 无需手动提取
cj = r.cookies,session会自动管理请求过程中的所有Cookie,直接使用session发起后续请求即可。 - 添加模拟浏览器的请求头,比如
User-Agent、Referer,避免被识别为非人工请求。
- 无需手动提取
填充必填的
authenticity_token
payload中该字段为空,这是Shopify的CSRF验证字段,必须从结账页面的HTML表单隐藏域中提取后填入,否则请求会被拦截,间接导致配送费率验证失败。修正字段拼写错误
payload中的g-recaptcha-repsonse拼写错误,正确应为g-recaptcha-response。若目标站点启用reCAPTCHA,需正确填充该字段的验证结果,否则无法完成结账。
内容的提问来源于stack exchange,提问作者imObjCSwifting
相关产品推荐
相关产品推荐

