2010-12-12 77 views
2

我正在嘗試使用以下代碼登錄到互聯網快速瀏覽。我的問題是,我如何成功登錄?我在代碼的最後打印出當前的URL,但是當我運行該腳本時,它只返回www.megaupload.com。使用機械化登錄到互聯網快照

import mechanize 
import cookielib 
from BeautifulSoup import BeautifulSoup 
import html2text 

# Browser 
br = mechanize.Browser() 

# Cookie Jar 
cj = cookielib.LWPCookieJar() 
br.set_cookiejar(cj) 

# Browser options 
br.set_handle_equiv(True) 
br.set_handle_gzip(True) 
br.set_handle_redirect(True) 
br.set_handle_referer(True) 
br.set_handle_robots(False) 

# Follows refresh 0 but not hangs on refresh > 0 
br.set_handle_refresh(mechanize._http.HTTPRefreshProcessor(), max_time=1) 

# User-Agent (this is cheating, ok?) 
br.addheaders = [('User-agent', 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.1) Gecko/2008071615 Fedora/3.0.1-1.fc9 Firefox/3.0.1')] 

# The site we will navigate into, handling it's session 
br.open('http://www.megaupload.com/?c=login') 

# Select the first (index zero) form 
br.select_form('loginfrm') 

#User credentials 
br.form['username'] = 'USERNAMEGOESHERE' 
br.form['password'] = 'PASSWORDGOESHERE' 

br.submit() 


#prints out the current log in 
print br.geturl() 

回答

3

搜索在響應主體錯誤消息:

"Username and password do not match" in br.response().read() 

或者檢查是否得到了預期的cookie(簡單的例子,調整需要的話):

any(c.domain == ".megaupload.com" and c.name == "user" for c in cj) 
+0

謝謝,其實我能夠檢查「歡迎用戶名」。我試圖加註你,但我是一個新用戶,在我這樣做之前我需要「15聲望」。 – ChrisC 2010-12-12 20:02:06

+0

@ user520574,你不能選擇它作爲答案嗎?好吧,沒關係。 – tokland 2010-12-12 20:04:19

+0

我能夠選擇它作爲答案,我之前沒有看到該選項。再次感謝。 – ChrisC 2010-12-12 20:17:28