问题描述
||
我试图在python中使用机械化来填写表格,但无法与正常的commit()一起使用。机械化无法以某种方式解析单选按钮,仅找到1而不是4。之后,我尝试编写POST请求-
data = {
\'localid\' : \'11755\',\'language\' : \'3\',\'code\' : \'hello world\',}
page = browser.open( self.submiturl,urllib.urlencode( data) )
但这根本不发布任何内容。我不确定我在这里缺少什么,这是进行POST的正确方法吗?还有其他方法可以使机械化识别单选按钮吗?
我的完整代码可以从此链接中读取。
解决方法
听起来像是机械化在解析表单时遇到了麻烦,请尝试按照以下方式进行操作
br = mechanize.Browser()
resp = br.open(\'your_url_here\')
print resp.get_data() # if you want to see what\'s returned
# if you want to see the forms,so you can find the index of the
# form you want and check that is has all the fields,if it doesn\'t
# you should should parse the response with BeautifulSoup
for form in br.forms():
print \'---------------\'
print form
br.select_form(nr=0) # to select the first form
br[\'field_name\'] = \'field_value\'
br[\'select_field_name\'] = [\'select_field_value\']
br.submit()