Python - 在表单中机械化输入文本

时间:2014-11-21 05:14:13

标签: python forms mechanize

我想在表单的文本字段中输入一些文本。这是我目前的代码。接下来我该怎么办?

import re
from mechanize import Browser

br = Browser()
br.open("xyz.com")
formcount=0
for frm in br.forms():  
if str(frm.attrs["id"])=="xyz":
break
formcount=formcount+1
br.select_form(nr=formcount)

## What should I code here to input text into the form?

response = br.submit() 

2 个答案:

答案 0 :(得分:2)

br.form['id'] = 'ss-form' # This does the input
br.submit() # This will submit the form
print br.response().read() # This will read the new page returned

尝试print br.response().read()。这就是你想要的,你可以用Beautiful Soup解析响应。 soup = BeautifulSoup(br.response().read())

答案 1 :(得分:0)

如果表单未命名,您可以使用:

br.select_form(nr=0)

这将选择第一个表格("第0个")。