【问题标题】:Using Mechanize (Python) to fill form使用 Mechanize (Python) 填写表格
【发布时间】:2014-04-13 05:18:22
【问题描述】:

我想使用 python mechanize 填写此页面上的表单,然后记录响应。我该怎么做?当我使用以下代码在此页面上搜索表单时,它仅显示用于搜索的表单。我应该如何使用名称、性别等字段找到另一个表单的表单名称?

http://aapmaharashtra.org/join-us

代码:

import mechanize
br=mechanize.Browser()
br.open("http://aapmaharashtra.org/join-us")
for form in br.forms():
    print "Form name:", form.name
    print form

【问题讨论】:

  • @alecxe 谢谢,它有帮助!我还有一个答案。在我选择任何州的表格中,地区字段中的选项会动态变化。我应该如何看待使用 Mechanize 的地区的新可能选项?

标签: python forms python-2.7 web-scraping mechanize


【解决方案1】:

您需要的表单(带有id="form1")是动态加载的 - 这就是您在select_forms() 结果中看不到它的原因。

通过使用浏览器开发工具,您可能会发现表单是从http://internal.aamaadmiparty.org/Directory/Format.aspx?master=blank 链接加载的。

你应该从这里开始:

import mechanize


br = mechanize.Browser()
br.open("http://internal.aamaadmiparty.org/Directory/Format.aspx?master=blank")
br.select_form(nr=0)

br.form['ctl00$MainContent$txtname'] = 'Test Name'
# fill out other fields

req = br.submit()

希望对您有所帮助。

【讨论】:

  • 附带说明 - 有时使用开发人员工具查看代码以找到表单发布到的终点会更容易,然后只需使用 requests 发布所需的信息。
猜你喜欢
  • 1970-01-01
  • 2018-09-24
  • 2011-10-26
  • 1970-01-01
  • 1970-01-01
  • 2012-11-26
  • 1970-01-01
  • 2019-10-17
  • 2017-03-10
相关资源
最近更新 更多