无法使用 Python 登录亚马逊 [英] Unable to log in to Amazon using Python

查看:29
本文介绍了无法使用 Python 登录亚马逊的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Python 3 编写脚本以登录 Amazon 以获取我的 Kindle 亮点.它基于这篇文章:https://blog.jverkamp.com/2015/07/02/scraping-kindle-highlights/

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/

我无法成功登录,而是收到一条消息,提示启用 cookie 以继续:

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

<RequestsCookieJar[<Cookie ubid-main=189-4768762-8531647 for .amazon.com/>]>
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.

我已经包含了处理 cookie 的请求会话,但它似乎不起作用.

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'

您也可以使用 css select 和 has_attr 来避免尝试:

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)

输出的第一行,最后可以看到Amazon Kindle: Home:

The first line of the output, you can see <title>Amazon Kindle: Home</title> at the end:

b'<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
  <head>
    <title>Amazon Kindle: Home</title>
  

如果它仍然无法正常工作,您应该更新您的请求版本,并可能尝试另一个用户代理.一旦我更改了 ap_emailap_password,我就可以正常登录了.

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.

这篇关于无法使用 Python 登录亚马逊的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆