如何使用python和机械化登录网站
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/16598145/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to login to a website with python and mechanize
提问by Rappel
i'm trying to log in to the website http://www.magickartenmarkt.deand do some analyzing in the member-area (https://www.magickartenmarkt.de/?mainPage=showWants). I saw other examples for this, but i don't get why my approaches didn't work. I identified the right forms for the first approach, but it's not clear if it worked. In the second approach the returing webpage shows me that i don't have access to the member area.
我正在尝试登录网站http://www.magickartenmarkt.de并在会员区 ( https://www.magickartenmarkt.de/?mainPage=showWants) 中进行一些分析。我看到了其他例子,但我不明白为什么我的方法不起作用。我为第一种方法确定了正确的形式,但不清楚它是否有效。在第二种方法中,返回网页显示我无权访问会员区。
I would by glad for any help.
我很乐意提供任何帮助。
import urllib2
import cookielib
import urllib
import requests
import mechanize
from mechanize._opener import urlopen
from mechanize._form import ParseResponse
USERNAME = 'Test'
PASSWORD = 'bla123'
URL      = "http://www.magickartenmarkt.de"
# first approach
request = mechanize.Request(URL)
response = mechanize.urlopen(request)
forms = mechanize.ParseResponse(response, backwards_compat=False)
# I don't want to close?!
#response.close()
# Username and Password are stored in this form
form = forms[1]
form["username"] = USERNAME
form["userPassword"] = PASSWORD
#proof entering data has worked
user = form["username"]  # a string, NOT a Control instance
print user
pw = form["userPassword"]  # a string, NOT a Control instance
print pw
#is this the page where I will redirected after login?
print urlopen(form.click()).read () 
#second approach
cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
login_data = urllib.urlencode({'username' : USERNAME, 'userPassword': PASSWORD})
#login
response_web = opener.open(URL, login_data)
#did it work? for me not....
resp = opener.open('https://www.magickartenmarkt.de/?mainPage=showWants')
print resp.read()
采纳答案by Ford
Why not use a browser instance to facilitate navigation? Mechanize also has the ability to select particular forms (e.g. nr = 0 will select the first form on the page)
为什么不使用浏览器实例来促进导航?Mechanize 还具有选择特定表单的能力(例如 nr = 0 将选择页面上的第一个表单)
browser = mechanize.Browser()
browser.open(YOUR URL)
browser.select_form(nr = 0)
browser.form['username'] = USERNAME
browser.form['password'] = PASSWORD
browser.submit()
回答by Natesh bhat
Web automation ? Definitely "WEBBOT"
网络自动化 ? 绝对是“WEBBOT”
webbotworks even for webpages with dynamically changing id and classnames and has more methods and features than selenium.
webbot甚至适用于具有动态更改的 id 和类名的网页,并且比 selenium 具有更多的方法和功能。
Here's a snippet :)
这是一个片段:)
from webbot import Browser 
web = Browser()
web.go_to('google.com') 
web.click('Sign in')
web.type('[email protected]' , into='Email')
web.click('NEXT' , tag='span')
web.type('mypassword' , into='Password' , id='passwordFieldId') # specific selection
web.click('NEXT' , tag='span') # you are logged in ^_^

