使用Mechanize(Python)填写表单

2024-10-06 12:37:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我想使用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

Tags: 代码nameorg定位brform名称http
1条回答
网友
1楼 · 发布于 2024-10-06 12:37:00

您需要的表单(带有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()

希望能有所帮助。

相关问题 更多 >