使用 Mechanize (Python) 填写表单

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22294489/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 00:42:45  来源:igfitidea点击:

Using Mechanize (Python) to fill form

pythonformspython-2.7web-scrapingmechanize

提问by user2684957

I want to fill the form on this page using python mechanize and then record the response. How should I do it? When I search for forms on this page using the following code, it shows the form only for the search. How should I locate the form name of the other form with fields such as name, gender etc?

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

http://aapmaharashtra.org/join-us

http://aapmaharashtra.org/join-us

Code:

代码:

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

采纳答案by alecxe

The form you need (with an id="form1") is loaded dynamically on the fly - this is why you don't see it in the select_forms()results.

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

By using browser developer tools you may find out that the form is loaded from http://internal.aamaadmiparty.org/Directory/Format.aspx?master=blanklink.

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

Here's what you should start with then:

以下是您应该开始的内容:

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()

Hope that helps.

希望有帮助。