我正在尝试使用亚马逊广告 API 获取亚马逊广告数据。我必须打开一个特定链接:“ https://www.amazon.com/ap/oa?client_id=...&scope=cpc_advertising:campaign_management&response_type=code&redirect_uri=https://.../ ”,将我重定向到一个网址,我可以在其中提供我的登录数据。
为了获得那个重定向 url,我在 Python 中使用 BeautifulSoup。在 Web Inspector 中,我看到重定向 url 位于名称“location”下的响应标头中,但是当使用 BeautifulSoup 调用时,“location”丢失了。
from bs4 import BeautifulSoup
headers = {'user-agent': 'Thats the User Agent I get from the Web Inspector'}
p = requests.get(
'Thats the link from which I wll be further redirected')
login_data = {
'appAction': 'SIGNIN',
'email': '...',
'password': '...'
}
with requests.Session() as s:
url = p.url
r = s.get(url, headers=headers)
soup = BeautifulSoup(r.content, 'html.parser')
login_data['appActionToken'] = soup.find('input', attrs={'name': 'appActionToken'})['value']
login_data['openid.return_to'] = soup.find('input', attrs={'name': 'openid.return_to'})['value']
login_data['prevRID'] = soup.find('input', attrs={'name': 'prevRID'})['value']
login_data['workflowState'] = soup.find('input', attrs={'name': 'workflowState'})['value']
r = s.post(url, data = login_data, headers=headers)
print(r.content)
我们可以从哪里获得重定向网址的任何想法?