2010-01-14 103 views
2

我試圖從網站上取消一些數據。 我想編寫的腳本,應該得到頁面的內容:使用Python /機械化select_form()時出錯?

http://www.atpworldtour.com/Rankings/Singles.aspx 

應模擬用戶去藥谷額外積分和日期,每一個選項,模擬點擊進入,然後獲取數據應使用後後臺功能。

現在我一直在努力,只是選擇此選項用於其他常委:

  <option value="101" >101-200</option> 

這裏是我的(差)試圖嘗試這樣做:

from mechanize import Browser 
from BeautifulSoup import BeautifulSoup 
import re 
import urllib2 



br = Browser(); 
br.open("http://www.atpworldtour.com/Rankings/Singles.aspx"); 
br.select_form(nr=0); 
br["r"] = "101"; 

response = br.submit(); 

但它只是失敗在select_form(nr = 0)上應該選擇第一種形式。

這是Python的返回日誌:

>>> from mechanize import Browser 
>>> 
>>> from BeautifulSoup import BeautifulSoup 
>>> import re 
>>> import urllib2 
>>> 
>>> 
>>> 
>>> br = Browser(); 
>>> br.open("http://www.atpworldtour.com/Rankings/Singles.aspx"); 
<response_seek_wrapper at 0x311bb48L whose wrapped object = <closeable_response 
at 0x311be88L whose fp = <socket._fileobject object at 0x0000000002C94408>>> 
>>> br.select_form(nr=0); 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
    File "build\bdist.win-amd64\egg\mechanize\_mechanize.py", line 505, in select_ 
form 
    File "build\bdist.win-amd64\egg\mechanize\_html.py", line 546, in __getattr__ 
    File "build\bdist.win-amd64\egg\mechanize\_html.py", line 559, in forms 
    File "build\bdist.win-amd64\egg\mechanize\_html.py", line 228, in forms 
mechanize._html.ParseError 

我找不到在機械化主頁的所有功能的適當的解釋。任何人都可以指向我一個正確的教程使用形式和機械化或幫助我在這個特定的問題?

安東尼

回答

1

我認爲你是正確使用的庫,但解析器似乎有與特定頁面的麻煩。我在另一頁上以相同的方式使用庫(「http://flashcarddb.com/login」),並且不會引發錯誤。

1

我剛碰到同樣的問題。我訪問的頁面通過W3C驗證,所以我不認爲這是一個標記問題。不過,html tidy抱怨說該頁面內有一個。一旦我修好了,機械就開始工作了。

另外,我在郵件列表上看到了對這個問題的回覆。我只想指出,將mechanize.RobustFactory()添加到mechanize.Browser()不會改變結果。

0

線索:定義更多關於您的mechanize.Browser()