gpt4 book ai didi

python Mechanize 表单提交错误

转载 作者:行者123 更新时间:2023-12-04 16:20:41 25 4
gpt4 key购买 nike

import mechanize

br=mechanize.Browser()
br.set_handle_robots(False)
formno=-1

br.open(raw_input("enter the url"))
allforms=br.forms()

for form in allforms:
print "form name:",form.name

print "lets get to work"

for form in br.forms():
formno=formno+1
print "*"*20
print "working on form ",form.name
#form.set_all_readonly(False)
print formno
br.select_form(nr=formno)
print "boss"
for control in br.form.controls:
if control.readonly is True:
pass
else:
print control.name
form[control.name]='sparsh'

resp=br.submit()
resp=resp.get_data()
print resp

输出:
enter the url http://www.howtogeek.com/
form name: None
form name: None
form name: None
lets get to work
********************
working on form None
0
boss
emailaddress
emailaddress False
email
email False

One more step!
Make sure to check your email for a confirmation message.

Note: If you do not get it soon, check your Spam or Junk folders.

hello
********************
working on form None
1

**Traceback (most recent call last):
File "D:\thesis\data\form_mechanize_try.py", line 21, in
br.select_form(nr=formno)
File "build\bdist.win32\egg\mechanize\_mechanize.py", line 524, in select_form
raise FormNotFoundError("no form matching "+description)
FormNotFoundError: no form matching nr 1**

为什么我收到这个错误?

我想要实现的是填写特定网站中可用的所有表格并查看响应..但上面的代码只能选择第一个表格,然后给出错误

最佳答案

您想使用未命名的表单。这是正确的语法:

# List the forms that are in the page

for form in br.forms():
print "Form name:", form.name
print form

# To go on the mechanize browser object must have a form selected
br.select_form(nd = "form1") # works when form has a name
br.form = list(br.forms())[0] # use when form is unnamed

Python 初学者有一个很棒的关于 Mechanize 的备忘单: http://www.pythonforbeginners.com/cheatsheet/python-mechanize-cheat-sheet/

关于python Mechanize 表单提交错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19356736/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com