python - 使用 Mechanize (Python) 输入到站点

标签 python html browser mechanize

我正在尝试使用 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 源代码中找到搜索栏的名称?

最佳答案

您可以使用 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

关于python - 使用 Mechanize (Python) 输入到站点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16072548/

相关文章:

Python 异步 : event loop does not seem to stop when stop method is called

javascript - Jquery/JavaScript : How to append the data by column dynamically to each row in jquery?

c# - 如何通过任何浏览器捕获访问过的 URL 及其 html

java - Java Applet代码可以看到吗?

html - CSS全屏背景图片尺寸

C# WebBrowser 控件 - 忽略网站安全警告

Python MySQLdb "INSERT INTO"问题

osx 上的 python/pip 错误

python - 使用 Tkinter 创建一个 GUI,用于创建文件夹并解压 ZIP 文件

javascript - jQuery 在悬停时显示/隐藏特定 <li></li> 中的 div