如何使用Python登录网站?

Bru*_*dy' 77 python automation httpclient webautomation

我该怎么做?我试图输入一些指定的链接(使用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" />
Run Code Online (Sandbox Code Playgroud)

这可能吗?

slo*_*oth 63

也许你想使用斜纹织物(它基于机械化).它很容易使用,应该能够做你想要的.

它将如下所示:

from twill.commands import *
go('http://mysite.org')

fv("1", "email-email", "blabla.com")
fv("1", "password-clear", "testpass")

submit('0')
Run Code Online (Sandbox Code Playgroud)

showforms()用于go(...)浏览要登录的站点后,可以使用列出所有表单.只需从python解释器中尝试一下.

  • Python 3.6是否有解决方案?斜纹似乎不支持Python 3.5或3.6。我尝试下载并使用`2to3`进行转换,但是现在尝试导入时收到`ModuleNotFoundError`。 (2认同)
  • 修复它是一种痛苦,但它的工作原理:/sf/answers/3182199611/ (2认同)

Tar*_*air 41

让我试着简单一点,假设网站的网址是www.example.com,你需要通过填写用户名和密码来注册,所以我们去登录页面说http://www.example.com/login .php现在查看它的源代码并搜索它将在表单标签中的操作URL

 <form name="loginform" method="post" action="userinfo.php">
Run Code Online (Sandbox Code Playgroud)

现在用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
Run Code Online (Sandbox Code Playgroud)

我希望有一天能帮到某个地方.


Ant*_*ggs 25

通常,您需要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())
Run Code Online (Sandbox Code Playgroud)

您不一定需要HTTPS或Redirect处理程序,但它们不会受到伤害,并且它使开启者更加强大.您也可能不需要cookie,但很难从您发布的表单中分辨出来.我怀疑你可能,纯粹来自被记录下来的"记住我"的输入.


blo*_*ley 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
Run Code Online (Sandbox Code Playgroud)

有关更多信息,请访问:https://docs.python.org/2/library/urllib2.html


Nat*_*hat 12

网页自动化?绝对是"webbot"

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 ^_^
Run Code Online (Sandbox Code Playgroud)

这些文档也很简单易用:https://webbot.readthedocs.io


Ale*_*lli 6

一般而言,网站可以通过多种不同的方式检查授权,但您所针对的网站似乎可以让您相当容易.

您只需要POSTauth/loginURL中找到一个表单编码的blob,其中包含您在那里看到的各种字段(忘记标签for,它们是人类访问者的装饰). handle=whatever&password-clear=pwd等等,只要您知道句柄(AKA电子邮件)和密码的值,您应该没问题.

据推测,POST会将您重定向到一些"您已成功登录"页面,其中包含Set-Cookie验证会话的标头(确保保存该cookie并将其发送回会话中的进一步交互!).


And*_*510 5

对于 HTTP 的东西,当前的选择应该是:Requests- HTTP for Humans