使用requests模块修改亚马逊配送地址失败问题排查
问题
我访问亚马逊英国站时,发现页面左上角的"Deliver to"选项会根据访问地区自动切换。我想把它切换为"United States",于是写了一段Python脚本尝试实现,但执行后输出还是"Deliver to Bangladesh",不是预期的"Deliver to United States"。
我尝试的代码如下:
import requests from bs4 import BeautifulSoup start_url = 'https://www.amazon.co.uk/' cookie_consent_url = 'https://www.amazon.co.uk/privacyprefs/retail/v1/acceptall' location_changer_url = 'https://www.amazon.co.uk/portal-migration/hz/glow/address-change?actionSource=glow' location_details_checker_url = 'https://www.amazon.co.uk/portal-migration/hz/glow/get-location-label?storeContext=generic&pageType=Gateway&actionSource=desktop-modal' headers = { 'User-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36', 'x-requested-with': 'XMLHttpRequest', 'accept': 'application/json', } params = { 'anti-csrftoken-a2z': '', 'accept': 'all' } payload = {"locationType":"COUNTRY","district":"US","countryCode":"US","storeContext":"generic","deviceType":"web","pageType":"Gateway","actionSource":"glow"} with requests.Session() as s: s.headers.update(headers) res = s.get(start_url) soup = BeautifulSoup(res.text,'lxml') params['anti-csrftoken-a2z'] = soup.select_one("input[name='anti-csrftoken-a2z']")['value'] resp = s.post(cookie_consent_url,data=params) res = s.post(location_changer_url,json=payload) response = s.get(location_details_checker_url) print(response.json()['deliveryShortLine'])
当前输出(显示脚本执行所在地区的默认国家):
Deliver to Bangladesh
期望输出:
Deliver to United States
解决方案
亚马逊的位置切换接口需要完整的参数匹配,且会话状态和请求头的细节直接影响请求有效性。以下是调整后的可运行代码及核心修改说明:
修改后的代码
import requests from bs4 import BeautifulSoup start_url = 'https://www.amazon.co.uk/' cookie_consent_url = 'https://www.amazon.co.uk/privacyprefs/retail/v1/acceptall' location_changer_url = 'https://www.amazon.co.uk/portal-migration/hz/glow/address-change' location_details_checker_url = 'https://www.amazon.co.uk/portal-migration/hz/glow/get-location-label?storeContext=generic&pageType=Gateway&actionSource=desktop-modal' headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36', 'Accept': 'application/json, text/plain, */*', 'Referer': start_url, 'X-Requested-With': 'XMLHttpRequest' } with requests.Session() as s: # 初始访问获取CSRF Token和基础会话Cookie res = s.get(start_url) soup = BeautifulSoup(res.text, 'lxml') csrf_token = soup.select_one("input[name='anti-csrftoken-a2z']")['value'] # 提交Cookie授权 consent_params = { 'anti-csrftoken-a2z': csrf_token, 'accept': 'all' } s.post(cookie_consent_url, data=consent_params, headers=headers) # 构造位置切换Payload,补充CSRF Token,调整district参数 location_payload = { "locationType": "COUNTRY", "countryCode": "US", "district": "", # 国家级切换无需填写具体州代码,留空即可 "storeContext": "generic", "deviceType": "web", "pageType": "Gateway", "actionSource": "glow", "anti-csrftoken-a2z": csrf_token # 必须携带CSRF Token验证请求合法性 } # 发送位置切换请求 location_res = s.post(location_changer_url, json=location_payload, headers=headers) # 获取更新后的位置信息 response = s.get(location_details_checker_url, headers=headers) print(response.json()['deliveryShortLine'])
核心修改点
- 补充CSRF Token到位置切换请求:原代码仅在Cookie授权中使用了CSRF Token,但位置切换接口同样需要该参数验证请求合法性,这是导致请求无效的核心原因。
- 完善请求头:添加
Referer字段模拟真实浏览器的访问链路,避免被亚马逊的反爬机制拦截。 - 调整Payload参数:国家级切换无需填写
district具体值,留空即可;原代码中填写的"US"并非合法的州代码,会导致参数不匹配。 - 简化接口URL:移除位置切换URL中多余的
?actionSource=glow参数,该参数已包含在Payload中。
额外注意事项
如果运行后仍显示原地区,大概率是亚马逊的IP地理位置识别机制在生效——即使通过接口切换了位置,亚马逊仍会根据你的实际IP地址返回对应地区信息。这种情况下需要配合美国地区的代理IP使用。
内容的提问来源于stack exchange,提问作者robots.txt
相关产品推荐
相关产品推荐

