0

我正在尝试使用dictionary.com 作为源代码制作字典程序,但在源代码中找不到搜索框的名称

from mechanize import Browser

inp = raw_input("Enter Word: ")
Word = (inp)

SEARCH_PAGE = "http://dictionary.reference.com/"

browser = Browser()
browser.open( SEARCH_PAGE )
browser.select_form( nr=0 ) 

browser['name of search form'] = Word
browser.submit()

谁能帮我完成这项工作或帮我在 HTML 源代码中找到搜索栏的名称?

4

1 回答 1

1

您可以使用 Browser.forms() 方法查看表单。这些表单中的每一个都有一个称为控件的变量,即表单中的控件列表。此列表中的每个控件都有一个“名称”变量。如您所知,您可以使用这些名称与浏览器建立索引。

from mechanize import Browser

inp = raw_input("Enter Word: ")
Word = (inp)

SEARCH_PAGE = "http://dictionary.reference.com/"

browser = Browser()
browser.open( SEARCH_PAGE )

form = list(browser.forms())[0]                        #The first form
print form
names = map(lambda n: n.name, form.controls)
print names

browser.select_form( nr=0 ) 

browser[names[0]] = Word

txt = browser.submit().read()

#txt has the html from dictionary.reference.com
于 2013-04-18T02:07:54.787 回答