简体   繁体   English

为什么我可以使用 python mechanize 登录亚马逊网站,但不能使用 requests 或 urllib2

[英]Why I can log in amazon website using python mechanize, but not requests or urllib2

I can use the following piece of python code found from here to log into amazon.com:我可以使用从这里找到的以下 Python 代码段登录到 amazon.com:

import mechanize 

br = mechanize.Browser()  
br.set_handle_robots(False)  
br.addheaders = [("User-agent", "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.13) Gecko/20101206 Ubuntu/10.10 (maverick) Firefox/3.6.13")]  

sign_in = br.open('https://www.amazon.com/gp/sign-in.html')  

br.select_form(name="sign-in")  
br["email"] = 'test@test.com' 
br["password"] = 'test4test'
logged_in = br.submit() 

orders_html = br.open("https://www.amazon.com/gp/css/history/orders/view.html?orderFilter=year-%s&startAtIndex=1000" % 2013)

But following two pieces using requests module and urllib2 do not work.但是以下两部分使用 requests 模块和 urllib2 不起作用。

import requests
import sys

username = "test@test.com"
password = "test4test"

login_data = ({
        'email' : fb_username,
        'password' : fb_password,
        'flex_password': 'true'})

url = 'https://www.amazon.com/gp/sign-in.html'

agent ={'User-agent', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_4) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.57 Safari/537.1'}

session = requests.session(config={'verbose': sys.stderr}, headers = agent)

r = session.get('http://www.amazon.com')

r1 = session.post(url, data=login_data, cookies=r.cookies)

r2 = session.post("https://www.amazon.com/gp/css/history/orders/view.html?orderFilter=year-2013&startAtIndex=1000", cookies = r1.cookies)

# #

import urllib2
import urllib
import cookielib

amazon_username = "test@test.com"
amazon_password = "test4test"
url = 'https://www.amazon.com/gp/sign-in.html'

cookie = cookielib.CookieJar()
login_data = urllib.urlencode({'email' : amazon_username, 'password' : amazon_password,})

opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookie))
opener.addheaders = [('User-agent', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_4) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.57 Safari/537.1')]

opener.open('www.amazon.com')

response = opener.open(url, login_data)

response = opener.open("https://www.amazon.com/gp/css/history/orders/view.html?orderFilter=year-%s&startAtIndex=1000" % 2013, login_data)

What did I do wrong in posting the amazon log in form?我在发布亚马逊登录表单时做错了什么? This is the first time I post a form.这是我第一次发布表格。 Any help is appreciated.任何帮助表示赞赏。

I prefer to use urllib2 or requests because all my other code are using these two modules.我更喜欢使用 urllib2 或 requests,因为我所有的其他代码都使用这两个模块。

Moreover, can any body comment on the speed performance between mechanize, requests and urllib2, and other advantage of mechanize over the other two?此外,任何机构都可以评论 mechanize、requests 和 urllib2 之间的速度性能,以及 mechanize 相对于其他两个的其他优势吗?

~~~~~~~~~~~New~~~~~~~~~~~~ Following CC's instruction, I now can log in with urllib2. ~~~~~~~~~~~新~~~~~~~~~~~~ 按照CC的指示,我现在可以用urllib2登录了。 But when I try to do the same with requests, it still does not work.但是当我尝试对请求做同样的事情时,它仍然不起作用。 Can anyone give me a clue?谁能给我一个线索?

import requests
import sys

fb_username = "test@test.com"
fb_password = "xxxx"

login_data = ({
            'email' : fb_username,
            'password' : fb_password,
            'action': 'sign-in'})

url = 'https://www.amazon.com/gp/sign-in.html'

agent ={'User-agent', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_4) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.57 Safari/537.1'}

session = requests.session(config={'verbose': sys.stderr}, headers = agent)

r = session.get(url)

r1 = session.post('https://www.amazon.com/gp/flex/sign-in/select.html', data=login_data, cookies=r.cookies)

b = r1.text

Regarding your urllib2 approach, you are missing 2 things.关于您的urllib2方法,您缺少两件事。

First, if you look at the source of sign-in.html , it shows that首先,如果您查看sign-in.html的来源,它表明

<form name="sign-in" id="sign-in" action="/gp/flex/sign-in/select.html" method="POST">

Meaning the form should be submitted to select.html .这意味着表单应该提交到select.html

Second, besides email & password, you also need to select whether you are an existing user or not:其次,除了邮箱和密码之外,您还需要选择您是否是现有用户:

<input id="newCust" type="radio" name="action" value="new-user"...>
...
<input id="returningCust" type="radio" name="action" value="sign-in"...>

It should look something like this:它应该是这样的:

import cookielib
import urllib
import urllib2

amazon_username = ...
amazon_password = ...

login_data = urllib.urlencode({'action': 'sign-in',
                               'email': amazon_username,
                               'password': amazon_password,
                               })

cookie = cookielib.CookieJar()    
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookie))
opener.addheaders = [('User-agent', ...)]

response = opener.open('https://www.amazon.com/gp/sign-in.html')
print(response.getcode())

response = opener.open('https://www.amazon.com/gp/flex/sign-in/select.html', login_data)
print(response.getcode())

response = opener.open("https://www.amazon.com/") # it should show that you are logged in
print(response.getcode())

I was able to login to Amazon using requests .我能够使用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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM