python亚马逊_无法使用Python登录Amazon

在尝试使用Python脚本登录Amazon获取Kindle高亮内容时遇到问题,提示需启用cookies。代码中已包含requests sessions处理cookies,但未能成功。经过检查,发现登录表单数据应为'email'和'password'而非'ap_email'和'ap_password'。更新这部分数据后,脚本能够正常登录。如果仍存在问题,可能需要更新requests库或尝试更换User-Agent。
摘要由CSDN通过智能技术生成

I'm using Python 3 to write a script to log in to Amazon to grab my Kindle highlights. It is based on this article: https://blog.jverkamp.com/2015/07/02/scraping-kindle-highlights/

I am unable to successfully log in and instead get a message saying to enable cookies to continue:

]>

Failed to login:

Please Enable Cookies to Continue

To continue shopping at Amazon.com, please enable cookies in your Web browser.

Learn more about cookies and how to enable them.

I have included requests sessions to handle cookies, but it doesn't seem to be working.

Here is the code I am using to try to do this:

import bs4, requests

session = requests.Session()

session.headers = {

'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.110 Safari/537.36'

}

# Log in to Amazon, we have to get the real login page to bypass CSRF

print('Logging in...')

response = session.get('https://kindle.amazon.com/login')

soup = bs4.BeautifulSoup(response.text, "html.parser")

signin_data = {}

signin_form = soup.find('form', {'name': 'signIn'})

for field in signin_form.find_all('input'):

try:

signin_data[field['name']] = field['value']

except:

pass

signin_data[u'ap_email'] = 'myemail'

signin_data[u'ap_password'] = 'mypassword'

response = session.post('https://www.amazon.com/ap/signin', data = signin_data)

soup = bs4.BeautifulSoup(response.text, "html.parser")

warning = soup.find('div', {'id': 'message_warning'})

if warning:

print('Failed to login: {0}'.format(warning.text))

Is there something I'm missing with my use of sessions?

解决方案

Your signin form data is actually not correct it should be email and password:

signin_data[u'email'] = 'your_email'

signin_data[u'password'] = 'your_password'

You can also avoid the try with a css select and has_attr:

import bs4, requests

headers = {

'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.110 Safari/537.36'

}

from bs4 import BeautifulSoup

with requests.Session() as s:

s.headers = headers

r = s.get('https://kindle.amazon.com/login')

soup = BeautifulSoup(r.content, "html.parser")

signin_data = {s["name"]: s["value"]

for s in soup.select("form[name=signIn]")[0].select("input[name]")

if s.has_attr("value")}

signin_data[u'email'] = 'your_em'

signin_data[u'password'] = 'pass'

response = s.post('https://www.amazon.com/ap/signin', data=signin_data)

soup = bs4.BeautifulSoup(response.text, "html.parser")

warning = soup.find('div', {'id': 'message_warning'})

if warning:

print('Failed to login: {0}'.format(warning.text))

print(response.content)

The first line of the output, you can see

Amazon Kindle: Home at the end:

b'<?xml version="1.0" encoding="utf-8"?>\n\n\n

\n Amazon Kindle: Home\n

If it is not working still, you should update your version of requests and maybe try another user-agent. Once I changed the ap_email and ap_password I logged in fine.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值