python mechanize forms() err



我使用Python 2.7.6和mechanize 0.2.5,我想登录到'dining.ut.ac.ir'(我有用户名和密码)-但是当我尝试运行下面的脚本来获取forms list:

import mechanize
br = mechanize.Browser()
br.set_handle_robots(False)
br.addheaders = [('User-agent', 'Firefox')]    
br.open("http://dining.ut.ac.ir/")
br.forms()

我得到这个错误:

Traceback (most recent call last):
  File "script.py", line 8, in <module>
    br.forms()
  File "/home/arman/workspace/python/mechanize/venv/lib/python2.7/site-packages/mechanize/_mechanize.py", line 420, in forms
    return self._factory.forms()
  File "/home/arman/workspace/python/mechanize/venv/lib/python2.7/site-packages/mechanize/_html.py", line 557, in forms
    self._forms_factory.forms())
  File "/home/arman/workspace/python/mechanize/venv/lib/python2.7/site-packages/mechanize/_html.py", line 237, in forms
    _urlunparse=_rfc3986.urlunsplit,
  File "/home/arman/workspace/python/mechanize/venv/lib/python2.7/site-packages/mechanize/_form.py", line 844, in ParseResponseEx
    _urlunparse=_urlunparse,
  File "/home/arman/workspace/python/mechanize/venv/lib/python2.7/site-packages/mechanize/_form.py", line 981, in _ParseFileEx
    fp.feed(data)
  File "/home/arman/workspace/python/mechanize/venv/lib/python2.7/site-packages/mechanize/_form.py", line 758, in feed
    _sgmllib_copy.SGMLParser.feed(self, data)
  File "/home/arman/workspace/python/mechanize/venv/lib/python2.7/site-packages/mechanize/_sgmllib_copy.py", line 110, in feed
    self.goahead(0)
  File "/home/arman/workspace/python/mechanize/venv/lib/python2.7/site-packages/mechanize/_sgmllib_copy.py", line 144, in goahead
    k = self.parse_starttag(i)
  File "/home/arman/workspace/python/mechanize/venv/lib/python2.7/site-packages/mechanize/_sgmllib_copy.py", line 302, in parse_starttag
    self.finish_starttag(tag, attrs)
  File "/home/arman/workspace/python/mechanize/venv/lib/python2.7/site-packages/mechanize/_sgmllib_copy.py", line 347, in finish_starttag
    self.handle_starttag(tag, method, attrs)
  File "/home/arman/workspace/python/mechanize/venv/lib/python2.7/site-packages/mechanize/_sgmllib_copy.py", line 387, in handle_starttag
    method(attrs)
  File "/home/arman/workspace/python/mechanize/venv/lib/python2.7/site-packages/mechanize/_form.py", line 735, in do_option
    _AbstractFormParser._start_option(self, attrs)
  File "/home/arman/workspace/python/mechanize/venv/lib/python2.7/site-packages/mechanize/_form.py", line 480, in _start_option
    raise ParseError("OPTION outside of SELECT")
mechanize._form.ParseError: OPTION outside of SELECT

为什么我得到这个错误,我如何修复它?

您试图打开的URL是GZipped(使用此链接检查),所以您必须将gzipAccept-Encoding头附加到Browser:

import mechanize
br = mechanize.Browser()
br.set_handle_robots(False)
br.addheaders = [('User-agent', 'Firefox')]
br.addheaders.append( ['Accept-Encoding','gzip'] )
br.open("http://dining.ut.ac.ir/")
br.forms()

我以前遇到过同样的问题,这行代码解决了我的问题:

br = mechanize.Browser(factory=mechanize.RobustFactory())

那么,试试这个:

import mechanize
br = mechanize.Browser(factory=mechanize.RobustFactory())
br.set_handle_robots(False)
br.addheaders = [('User-agent', 'Firefox')]    
br.open("http://dining.ut.ac.ir/")
br.forms

相关内容

  • 没有找到相关文章

最新更新