我使用Python2.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为什么我会得到这个错误,以及如何修复它?
发布于 2014-03-21 21:02:48
您要打开的URL是GZipped (使用此链接检查它),因此您必须将gzip的Accept-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()发布于 2014-03-21 20:16:36
我之前也遇到过同样的问题,这一行代码解决了我的问题:
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.formshttps://stackoverflow.com/questions/22568532
复制相似问题