我该怎么做呢?我试图进入一些指定的链接(使用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" />
这个是可能的吗?
发布于 2010-05-26 13:38:59
也许你想使用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解释器中尝试一下。
发布于 2015-02-20 20:01:35
让我试着让它变得简单,假设站点的网址是www.example.com,你需要通过填写用户名和密码来注册,所以我们转到登录页面,说http://www.example.com/login.php now,并查看它的源代码,并搜索操作网址,它将是类似于以下形式的标签
<form name="loginform" method="post" action="userinfo.php">
现在将userinfo.php设置为'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
我希望这能在某一天对某个人有所帮助。
发布于 2010-05-26 14:19:06
通常你需要cookies来登录一个站点,这意味着cookies,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或重定向处理程序,但它们没有坏处,而且它使打开程序更加健壮。您也可能不需要cookie,但仅从您发布的表单很难判断。我怀疑你可能会,纯粹从‘记住我’输入,已经被注释掉。
https://stackoverflow.com/questions/2910221
复制相似问题