Python: 机械化 select_form returns 无

Python: Mechanize select_form returns nothing

当尝试使用 mechanize 时,我似乎无法使用 select_form(id) 来检索表单,即使在 forms() 中有一个对象生成器。

代码:

import mechanize

urls = ['http://whosebug.com/']

for url in urls:
    print url
    br = mechanize.Browser()
    br.addheaders = [('User-Agent', 'Firefox')]
    br.open(url)

    for form in br.forms():
        print form
    print br.select_form(nr=0)

输出:

http://whosebug.com/
<GET http://whosebug.com/search application/x-www-form-urlencoded
  <TextControl(q=)>>
None

它实际上选择了表格,因为它没有return任何错误。

你看到 None 的原因是 select_form() return 你 None.


作为一个单独的说明,为什么不使用 StackExchange API 而不是使用 mechanize 浏览?