如何使用BeautifulSoup登录到Amazon [英] How to login to Amazon using BeautifulSoup

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

问题描述

请参阅此帖子:无法使用Python登录到Amazon

我尝试使用建议的答案,但仍然无法登录.

I tried using the suggested answer, but still cannot login.

我添加了代码以显示结果.它将电子邮件输入到框中,但是结果中仍然看到输入有效的电子邮件".我敢肯定我正确选择了表单,并且输入字段的名称正确,但是需要一些指导来调试它.

I added code to display what the result is. It's inputting the email into the box, but I'm still seeing "Enter a valid email" in the result. I'm pretty sure I selected the form correctly, and the name's of the input fields are correct, but need a little guidance to debug this.

import bs4, requests
import os
import webbrowser


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://www.amazon.com/ap/signin?_encoding=UTF8&ignoreAuthState=1&openid.assoc_handle=usflex&openid.claimed_id=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.identity=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.mode=checkid_setup&openid.ns=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0&openid.ns.pape=http%3A%2F%2Fspecs.openid.net%2Fextensions%2Fpape%2F1.0&openid.pape.max_auth_age=0&openid.return_to=https%3A%2F%2Fwww.amazon.com%2F%3Fref_%3Dnav_signin&switch_account=')
    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'] = 'xx'
    signin_data[u'password'] = 'xx'

    response = s.post('https://www.amazon.com/ap/signin', data=signin_data)
    soup = bs4.BeautifulSoup(response.text, "html.parser")

html = response.content

path = os.path.abspath('temp.html')
url = 'file://' + path

with open(path, 'w') as f:
    f.write(str(html))
webbrowser.open(url)

推荐答案

我不了解BeautifulSoup,但是这是我使用requests的方法.

I don't know about BeautifulSoup, but here's how I did it using requests.

from getpass import getpass
import webbrowser
import requests
import os


amazon_username = raw_input("Amazon email: ")
amazon_password = getpass()

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",
    "action": "sign-in",
    "email": amazon_username,
    "password": amazon_password
    }

r = requests.get("https://www.amazon.com/gp/sign-in.html", headers=headers)
print(r.status_code)

r = requests.get("https://www.amazon.com/gp/flex/sign-in/select.html", headers=headers)
print(r.status_code)

r = requests.get("https://www.amazon.com/", headers=headers)
print(r.status_code)

这篇关于如何使用BeautifulSoup登录到Amazon的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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