使用Mechanize(Python)填写表单

use*_*957 8 python forms mechanize web-scraping python-2.7

我想使用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
Run Code Online (Sandbox Code Playgroud)

ale*_*cxe 10

您需要的表单(带有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()
Run Code Online (Sandbox Code Playgroud)

希望有所帮助.

  • 在旁注 - 有时使用开发人员工具查找代码更容易查找表单发布到的终点,然后只使用[requests](http://docs.python-requests.org/en/latest/ )发布所需信息. (4认同)