2015-10-19 93 views
0

我閱讀本頁面和代碼不仍能正常工作: Logging in to vBulletin連接vBulletin 4.2.2論壇與Python

我的代碼:

#-*- coding:utf-8 -*- 

import urllib, urllib2, cookielib, hashlib, time 

def variables(): 
    domain = "www.example.com" 
    uname = "UserName" 
    passwd = "Password" 

    # Create url 
    if domain.startswith('http://'): 
     url = domain 
    else: 
     url = 'http://' + domain 

    login(url, uname, passwd) 


def login(url, uname, passwd): 
    loginurl = url + '/login.php?do=login' 
    md5 = hashlib.md5(passwd);md5 = md5.hexdigest() 
    # Options for request 
    opts = { 
     'do': 'login', 
     'vb_login_md5password': md5, 
     'vb_login_md5password_utf': md5, 
     's': '', 
     'vb_login_username': uname, 
     'security_token': 'guest', 
    } 
    data = urllib.urlencode(opts) 

    # Request header 
    global headers 
    headers = { 
     'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 
     'Accept-Language': 'es-es,es;q=0.8,en-us;q=0.5,en;q=0.3', 
     'Accept-Encoding': 'gzip,deflate', 
     'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.7', 
     'Connection': 'keep-alive', 
     'Referer': loginurl, 
     'Content-Type': 'application/x-www-form-urlencoded', 
     'Content-Lenght': '205' 
    } 

    # Cookie Handling 
    jar = cookielib.CookieJar() 
    opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(jar)) 

    # Send Request 
    opener.addheader = headers 
    opener.open(loginurl, data) 

    # Check 
    response = opener.open('http://example.com/') 
    source_code = response.read() 
    if source_code.find(uname) != -1: 
     print "Login Succeeded." 
    else: 
     print "Login Failed." 


variables() 

字段名的屬性值是無(空):

<input type="hidden" name="s" value="" /> 

爲什麼代碼不起作用?

謝謝。

回答

0

缺少「vb_login_password」,「vb_login_password_hint」,「cookieuser」和「vb_login_md5password_utf」必須爲空。

並確保刪除您的標題。

+0

我應該把什麼vb_login_password_hint和cookieuser? 如果你可以給我例子的代碼,這將是非常有益的。 – Jonathan

+0

安裝Live HTTP Headers(只有firefox會工作)並打開它然後開始登錄,你會看到所有的POST請求。 – SethSandaru

+0

我在firefox中安裝了Live HTTP Header,並在開始登錄之前將其打開,並且我看到了所有POST請求,之後我相應地填寫了所有輸入表單,並且我試圖再次登錄而沒有成功。哪裏不對? – Jonathan