2012-04-06 27 views
0

我知道有不少人問這樣一個類似的問題,但看着雖然答案,下面這個提示我不能讓這個腳本工作後...Python和機械化登錄到網頁大學

這是我的問題...我正在嘗試使用「機械化」模塊來登錄我的大學「膳食平衡」頁面,並獲取頁面的源html,顯示我食物的餘額不斷下降,然後我會解析HTML源代碼,並獲得數...

問題正在訪問的網頁說,並登錄...

這個我S中的登錄網站:http://www.wcu.edu/11407.asp 迎你會看到我需要填寫表格結束...

這裏是我試圖以用於登錄並與我的餘額遞減獲得頁面的代碼:

import mechanize, cookielib 
from time import sleep 

url = 'http://www.wcu.edu/11407.asp' 
myId = 'xxxxxxxx' 
myPin = 'xxxxxxxx' 

# Browser 
#br = mechanize.Browser() 
#br = mechanize.Browser(factory=mechanize.DefaultFactory(i_want_broken_xhtml_support=True)) 
br = mechanize.Browser(factory=mechanize.RobustFactory()) # Use this because of bad html 

# 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 (fake agent to google-chrome linux x86_64)       
br.addheaders = [('User-agent','Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11'), 
       ('Accept', 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'), 
       ('Accept-Encoding', 'gzip,deflate,sdch'),      
       ('Accept-Language', 'en-US,en;q=0.8'),       
       ('Accept-Charset', 'ISO-8859-1,utf-8;q=0.7,*;q=0.3')] 

# The site we will navigate into, handling it's session 
br.open(url) 

for f in br.forms(): 
    print f 


# Select the third (index two) form 
br.select_form(nr=2) 

# User credentials 
br.form['id'] = myId 
br.form['PIN'] = myPin 

br.form.action = 'https://itapp.wcu.edu/BanAuthRedirector/Default.aspx 

# Login 
res = br.submit().read() 

sleep(10) 

f = file('mycatpage.html', 'w') 
f.write(res) 
f.close() 

這給了我登錄頁面回來,而不是頁面後....爲什麼?

回答

0

爲什麼不通過將代碼輸入到python shell來檢查錯誤來自哪裏?或者用另一個網站進行測試?有很多顯而易見的可能性來針對您所面臨的錯誤進行測試。

+0

錯誤源於提交,這正是問題,它在其他網站上正常工作...這就是爲什麼我發佈這個,也許有人可以找到我錯過的東西.... – Perun 2012-04-06 18:52:10

0

看看我的問題here

而且自動登錄了我的大學的網頁,與工作代碼和HTML代碼作爲例子。