【问题标题】:Problems with Forms, using POST method with Mechanize in Python表单问题,在 Python 中使用带有 Mechanize 的 POST 方法
【发布时间】:2011-09-13 18:49:28
【问题描述】:

我试图在 python 中使用 mechanize 填写表单,但它不适用于正常的 submit()。不知何故,mechanize 无法解析单选按钮,只找到 1 而不是 4。之后我尝试编写 POST 请求 -

    data = {
        'localid' : '11755',
        'language' : '3',
        'code' : 'hello world',
    }
    page = browser.open( self.submiturl, urllib.urlencode( data) )

但它根本没有发布任何内容。我不确定我在这里缺少什么,这是发帖的正确方法吗?有没有其他方法可以让 mechanize 识别单选按钮?

我的完整代码可以从this link阅读。

【问题讨论】:

标签: python forms post radio-button mechanize


【解决方案1】:

听起来 mechanize 在解析表单时遇到了问题,请尝试以下方法

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

【讨论】:

  • 非常感谢。有效。一旦我使用你的方式编写它,它就会正确解析单选按钮。
猜你喜欢
  • 2014-04-12
  • 1970-01-01
  • 2016-01-06
  • 2011-06-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多