2010-05-26 143 views
53

我該怎麼做? 我試圖進入一些指定的連接(與urllib的),但要做到這一點,我需要登錄如何使用Python登錄到網站?

我從網站這個來源:

<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" /> 

這可能嗎?

回答

50

也許你想使用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解釋器中嘗試一下。

+0

請注意,在某些情況下,您需要使用submit()。請參閱:http://lists.idyll.org/pipermail/twill/2006-August/000526.html我確認這個問題,對我來說,使用submit()作品登錄www.pge.com。 – user391339 2014-09-11 07:47:41

+0

是否有Python 3.6的解決方案?它似乎斜紋不支持Python 3.5或3.6。我試着下載它並使用'2to3'來轉換它,但是現在當我試圖導入它時,我得到了'ModuleNotFoundError'。 – CGFoX 2017-08-02 11:04:53

+0

實際上,我可以通過使用/轉換Twill 1.8.0來解決'ModuleNotFoundError',並用'pip install'安裝'lxml'和'requests'。 但是現在當我嘗試導入時出現'SyntaxError',因爲某處False = 0 .... – CGFoX 2017-08-02 11:18:32

5

網站一般可以在許多不同的方式查詢的授權,但您所指定的一個似乎令人相當容易給你。

所有你需要的是POSTauth/login的URL一個窗體編碼的blob與你在那裏看到的各個領域(忘記標籤for,它們是人類訪客的裝飾)。 handle=whatever&password-clear=pwd等等,只要你知道句柄(AKA電子郵件)和密碼的值,你應該沒問題。

假設POST會將您重定向到某個「您已成功登錄」的頁面,並帶有一個Set-Cookie標題驗證您的會話(請務必保存該cookie並在會話進一步交互時將其發回)。

14
import cookielib 
import urllib 
import urllib2 

url = 'http://www.someserver.com/auth/login' 
values = {'email-email' : '[email protected]', 
      '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

+0

鏈接不工作:在'docs.python.org'網址中添加了'2':https://docs.python.org/2/library/urllib2.html – 2016-08-31 17:54:05

21

通常你需要餅乾登錄到一個站點,這意味着cookielib,urllib而urllib2的。下面是一類我寫回來的時候我玩Facebook的網頁遊戲:

import cookielib 
import urllib 
import urllib2 

# set these to whatever your fb account is 
fb_username = "[email protected]" 
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或重定向處理,但只要不傷害,它使首戰更強大。你也可能不需要cookies,但很難從你發佈的表單中分辨出來。我懷疑你可能純粹是從「記住我」輸入中被註釋掉的。

33

讓我儘量做到簡單,現場的假設URL是www.example.com,你需要填寫用戶名和密碼註冊,所以我們去到登錄頁面說http://www.example.com/login.php現在並查看它的源代碼和搜索行動網址它會在表單標籤類似

<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 

我希望這可以幫助別人的地方一天。

+0

這對大多數人不起作用我嘗試使用的網站 – 2016-08-26 03:39:45

+0

在兩個幫助/ stackoverflow頁面中,我看到這是在我需要的一個站點上工作的唯一解決方案。 – Buoy 2017-04-19 02:10:43