2010-04-06 35 views
29

我試圖從機械化頁面中選擇一個表單,但是問題表單在html中沒有「name」屬性。我該怎麼辦?當我嘗試使用Python機械化選擇一個沒有名字的表格

br.select_form(name = "") 

我得到的錯誤,沒有聲明與該名稱的形式,該功能需要一個名稱輸入。網頁上只有一種表單,是否有其他方式可以選擇該表單?

回答

53

嘗試:

br.select_form(nr=0) 

選擇第一種形式

在機械化source

def select_form(self, name=None, predicate=None, <b>nr=None</b>): 
    """ 
    ... 
    nr, if supplied, is the sequence number of the form (where 0 is the 
    first). 
    """ 
+0

謝謝。這隻適用於我的實例只有一種形式。出於好奇,你認爲它可以用多種形式來完成?要麼是所有未命名的,要麼是一些已命名的,還有一些未命名的? – 2010-04-06 04:13:00

+1

@mvid,是的,一個文件可以有多種形式,名稱也是可選的,機械化應該沒有問題。 – YOU 2010-04-06 04:18:05

+0

我們可以從表單中獲得謂詞值嗎? – 2010-11-03 16:17:39

0

如果你想不管他們的名字,你執行了多種形式碼可以遍歷每個表單,讓腳本知道下一個表單將工作。

currentForm = 0 
for form in br.forms(): # Iterate over the forms 
     br.select_form(nr = currentForm) # Select the form 
     ''' 
     The code you want to run for every form 
     ''' 
     currentForm += 1 # Add 1 to the current working form so the script knows what form is working next 
相關問題