我该怎么办? 我试图输入一些指定的链接(使用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" />
这可能吗?
答案 0 :(得分:58)
也许你想使用twill(它基于mechanize)。它很容易使用,应该能够做你想做的事。
它将如下所示:
from twill.commands import *
go('http://mysite.org')
fv("1", "email-email", "blabla.com")
fv("1", "password-clear", "testpass")
submit('0')
使用showforms()
浏览到要登录的网站后,可以使用go(...)
列出所有表单。只需从python解释器中试一试。
答案 1 :(得分:40)
让我试着简单一点,假设该网站的网址是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
我希望有一天能帮助某个人。
答案 2 :(得分:23)
通常,您需要使用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,但很难从您发布的表单中分辨出来。我怀疑你可能,纯粹来自“记住我”的输入已被注释掉。
答案 3 :(得分:18)
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
答案 4 :(得分:8)
webbot
甚至可以处理具有不断变化的ID和类名并且比硒或机械化具有更多方法和功能的网页。
这是一个代码段:)
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
答案 5 :(得分:6)
网站一般可以通过多种不同的方式检查授权,但您定位的网站似乎可以让您轻松获得授权。
您需要的只是POST
auth/login
网址表格编码的blob,其中包含您在那里看到的各种字段(忘记标签for
,它们是人类访问者的装饰)。 handle=whatever&password-clear=pwd
等等,只要您知道句柄(AKA电子邮件)和密码的值,就应该没问题。
据推测,POST会将您重定向到一些“您已成功登录”页面,并使用Set-Cookie
标头验证您的会话(请确保保存该Cookie并将其发送回会话中的进一步互动!)
答案 6 :(得分:4)
对于HTTP事物,当前的选择应该是:Requests- HTTP for Humans