python机械化forms()错误 [英] python mechanize forms() err

查看:90
本文介绍了python机械化forms()错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using Python 2.7.6 and mechanize 0.2.5 and I want to log in to 'dining.ut.ac.ir' (I have the username and password)- but when I try to run the below script to get the 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

为什么会出现此错误以及如何解决?

Why I get this error and how I can fix it?

推荐答案

您尝试打开的URL已压缩(使用此链接),因此您必须将gzipAccept-Encoding标头附加到Browser:

The URL you are trying to open is GZipped (check it using this link), So you have to append Accept-Encoding header for gzip to your 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()

这篇关于python机械化forms()错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆