Python Mechanize select a form with no name

孤人 提交于 2019-12-18 01:30:13

问题


I am attempting to have mechanize select a form from a page, but the form in question has no "name" attribute in the html. What should I do? when I try to use

br.select_form(name = "")

I get errors that no form is declared with that name, and the function requires a name input. There is only one form on the page, is there some other way I can select that form?


回答1:


Try:

br.select_form(nr=0)

to select the first form

In Mechanize 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).
    """



回答2:


If you want to execute code for multiple forms no matter what their name is, you can loop over every form letting your script knowing which form will work next.

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


来源:https://stackoverflow.com/questions/2582580/python-mechanize-select-a-form-with-no-name

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!