[英]How can I login to a website with Python?
我该怎么做? 我试图输入一些指定的链接(使用 urllib),但要做到这一点,我需要登录。
我有这个网站的来源:
<form id="login-form" action="auth/login" method="post">
<div>
<!--label for="rememberme">Remember me</label><input type="checkbox" class="remember" checked="checked" name="remember me" /-->
<label for="email" id="email-label" class="no-js">Email</label>
<input id="email-email" type="text" name="handle" value="" autocomplete="off" />
<label for="combination" id="combo-label" class="no-js">Combination</label>
<input id="password-clear" type="text" value="Combination" autocomplete="off" />
<input id="password-password" type="password" name="password" value="" autocomplete="off" />
<input id="sumbitLogin" class="signin" type="submit" value="Sign In" />
这可能吗?
也许您想使用twill 。 它很容易使用,应该能够做你想做的事。
它将如下所示:
from twill.commands import *
go('http://example.org')
fv("1", "email-email", "blabla.com")
fv("1", "password-clear", "testpass")
submit('0')
使用go…
浏览到要登录的站点后,您可以使用showforms()
列出所有表单。 只需从 python 解释器中尝试一下。
简单说一下,假设网站的网址是www.example.com,你需要填写用户名和密码来注册,所以我们去登录页面说http://www.example.com/login .php现在查看它的源代码并搜索操作 URL 它将在表单标签中
<form name="loginform" method="post" action="userinfo.php">
现在使用 userinfo.php 生成绝对 URL,即“ http://example.com/userinfo.php ”,现在运行一个简单的 python 脚本
import requests
url = 'http://example.com/userinfo.php'
values = {'username': 'user',
'password': 'pass'}
r = requests.post(url, data=values)
print r.content
我希望这能在某天帮助某人。
通常,您需要 cookie 来登录站点,这意味着 cookielib、urllib 和 urllib2。 这是我在玩 Facebook 网络游戏时写回的一堂课:
import cookielib
import urllib
import urllib2
# set these to whatever your fb account is
fb_username = "your@facebook.login"
fb_password = "secretpassword"
class WebGamePlayer(object):
def __init__(self, login, password):
""" Start up... """
self.login = login
self.password = password
self.cj = cookielib.CookieJar()
self.opener = urllib2.build_opener(
urllib2.HTTPRedirectHandler(),
urllib2.HTTPHandler(debuglevel=0),
urllib2.HTTPSHandler(debuglevel=0),
urllib2.HTTPCookieProcessor(self.cj)
)
self.opener.addheaders = [
('User-agent', ('Mozilla/4.0 (compatible; MSIE 6.0; '
'Windows NT 5.2; .NET CLR 1.1.4322)'))
]
# need this twice - once to set cookies, once to log in...
self.loginToFacebook()
self.loginToFacebook()
def loginToFacebook(self):
"""
Handle login. This should populate our cookie jar.
"""
login_data = urllib.urlencode({
'email' : self.login,
'pass' : self.password,
})
response = self.opener.open("https://login.facebook.com/login.php", login_data)
return ''.join(response.readlines())
您不一定需要 HTTPS 或 Redirect 处理程序,但它们不会受到伤害,并且它使开启器更加健壮。 您可能也不需要 cookie,但仅从您张贴的表格中很难判断。 我怀疑您可能纯粹是从已注释掉的“记住我”输入中得出的。
webbot
甚至可以处理具有动态更改 id 和类名的网页,并且具有比 selenium 或 mechanize 更多的方法和功能。
这是一个片段:)
from webbot import Browser
web = Browser()
web.go_to('google.com')
web.click('Sign in')
web.type('mymail@gmail.com' , 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 ^_^
这些文档也非常简单易用: https : //webbot.readthedocs.io
import cookielib
import urllib
import urllib2
url = 'http://www.someserver.com/auth/login'
values = {'email-email' : 'john@example.com',
'password-clear' : 'Combination',
'password-password' : 'mypassword' }
data = urllib.urlencode(values)
cookies = cookielib.CookieJar()
opener = urllib2.build_opener(
urllib2.HTTPRedirectHandler(),
urllib2.HTTPHandler(debuglevel=0),
urllib2.HTTPSHandler(debuglevel=0),
urllib2.HTTPCookieProcessor(cookies))
response = opener.open(url, data)
the_page = response.read()
http_headers = response.info()
# The login cookies should be contained in the cookies variable
更多信息请访问: https : //docs.python.org/2/library/urllib2.html
网站通常可以通过许多不同的方式检查授权,但您所针对的一种方式似乎让您相当容易。
所有你需要的是POST
到auth/login
与您看到有(忘了标签的各个领域URL形式编码的BLOB for
,他们为人类的游客饰)。 handle=whatever&password-clear=pwd
等等,只要您知道句柄(又名电子邮件)和密码的值,您应该没问题。
据推测,POST 会将您重定向到某个“您已成功登录”页面,并带有验证您的会话的Set-Cookie
标头(确保保存该 cookie 并将其发送回会话中的进一步交互!)。
对于 HTTP 的东西,当前的选择应该是: Requests- HTTP for Humans
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.