使用机械化(Python)填写表单 [英] Using Mechanize (Python) to fill form

查看:350
本文介绍了使用机械化(Python)填写表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



http://aapmaharashtra.org/join-us

代码:

 进口机械化
br = mechanize.Browser()
br.open(http://aapmaharashtra.org/join-us)
for br.forms()中的表单:
print表单名称:,form.name
print form


您需要的表单(带有 id =form1)是动态加载的 - 这就是为什么您在 select_forms() results。



通过使用浏览器开发工具,您可能会发现表单是从 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'] ='测试名称'
#填写其他字段

req = br.submit()

希望有帮助。


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?

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

解决方案

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.

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

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.

这篇关于使用机械化(Python)填写表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆