gpt4 book ai didi

python Mechanize 表单()错误

转载 作者:太空宇宙 更新时间:2023-11-04 01:18:56 25 4
gpt4 key购买 nike

我正在使用 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 是 GZip 压缩的(使用 this link 进行检查),因此您必须将 gzipAccept-Encoding header 附加到您的 浏览器:

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

关于python Mechanize 表单()错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22568532/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com