最关键是获取请求中的anti-csrftoken-a2z参数,该参数需要请求两次才能获取到正确的数据
代码如下:
import requests
import base64
import random
from bs4 import BeautifulSoup
import re
import json
from requests.api import head
username = 'yourusername'
password = 'yourpassword'
port = 22225
session_id = random.random()
super_proxy_url = ('http://%s%s-session-%s:%s@zproxy.lum-superproxy.io:%d' %
(username, '-country-us', session_id, password, port))
proxies = {
'http': super_proxy_url,
'https': super_proxy_url
}
cc = base64.encodestring(b'https://www.amazon.de/shoppingPortal/desktop').decode()
s = requests.Session()
s.proxies = proxies
headers = {
'authority': 'www.amazon.de',
'sec-ch-ua': '" Not A;Brand";v="99", "Chromium";v="90", "Google Chrome";v="90"',
'sec-ch-ua-mobile': '?0',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36',
'content-type': 'application/x-www-form-urlencoded',
'accept': 'text/html,*/*',
'x-requested-with': 'XMLHttpRequest',
'contenttype': 'application/x-www-form-urlencoded;charset=utf-8',
'downlink': '10',
'ect': '4g',
'origin': 'https://www.amazon.de',
'sec-fetch-site': 'same-origin',
'sec-fetch-mode': 'cors',
'sec-fetch-dest': 'empty',
'referer': 'https://www.amazon.de/',
'accept-language': 'zh-CN,zh;q=0.9',
}
r = s.get('https://www.amazon.de/', verify=False, headers= headers)
params = {'pl': {
"adPlacementMetaData":{
"pageUrl": cc,
"adElementId":"ape_Gateway_right-2_desktop_placement",
"pageType":"Gateway",
"slotName":"right-2"
},
"adCreativeMetaData":{"adNetwork":"cs"}
}
}
soup = BeautifulSoup(r.text, 'lxml')
csrf = json.loads(soup.find('span', id = 'nav-global-location-data-modal-action').get('data-a-modal'))['ajaxHeaders']['anti-csrftoken-a2z']
headers['anti-csrftoken-a2z'] = csrf
s.get('https://www.amazon.de/gp/aq-feedback/lazyLoad/handler/af-link-handler.html', params = params, headers=headers)
r = s.get('https://www.amazon.de/gp/glow/get-address-selections.html?deviceType=desktop&pageType=Gateway&storeContext=NoStoreName&actionSource=desktop-modal', headers = headers)
data = re.search(r'CSRF_TOKEN : \"([\S]+)\",', r.text).group(1)
headers['anti-csrftoken-a2z'] = data
payload='locationType=LOCATION_INPUT&zipCode=14641&storeContext=generic&deviceType=web&pageType=Gateway&actionSource=glow&almBrandId=undefined'
r = s.post("https://www.amazon.de/gp/delivery/ajax/address-change.html", data = payload, headers = headers)
print('r1 text: ', r.text)
r4 = s.get('https://www.amazon.de/', verify=False, headers = headers)
soup = BeautifulSoup(r4.text, 'lxml')
with open('逆向amazon修改地址邮编保存的测试的html.html', 'w', encoding='utf8') as f:
f.write(str(soup))
zipCode可以修改为你要改的邮编。这里我使用了14641 。
由于使用了代理,所以代理你也需要更换的。