Python Mechanize在网站html中遇到问题

Python Mechanize在网站html中遇到问题,python,html,mechanize,Python,Html,Mechanize,我试图使用python模块mechanize在网页上填写表单,然后下载生成的html。网站如下: “xxx” 但首先,我只想列出表格。我的代码如下: import mechanize br = mechanize.Browser() br.set_handle_robots(False) # ignore robots br.set_handle_refresh(False) # can sometimes hang without this url = "xxx" response

我试图使用python模块mechanize在网页上填写表单,然后下载生成的html。网站如下:

“xxx”

但首先,我只想列出表格。我的代码如下:

import mechanize

br = mechanize.Browser()
br.set_handle_robots(False)   # ignore robots
br.set_handle_refresh(False)  # can sometimes hang without this

url = "xxx"
response = br.open(url)
a = response.read()      # the text of the page

for form in br.forms():
    print "Form name:", form.name
    print form
给出的错误列表非常大:

In [75]: % run -i form_filler.py
---------------------------------------------------------------------------
ParseError                                Traceback (most recent call last)
/home/blake/Python/form_filler.py in <module>()
     19 
     20 
---> 21 for form in br.forms():
     22         print "Form name:", form.name
     23         print form

/usr/local/lib/python2.7/dist-packages/mechanize-0.2.5-py2.7.egg/mechanize/_mechanize.pyc in forms(self)
    418         if not self.viewing_html():
    419             raise BrowserStateError("not viewing HTML")
--> 420         return self._factory.forms()
    421 
    422     def global_form(self):

/usr/local/lib/python2.7/dist-packages/mechanize-0.2.5-py2.7.egg/mechanize/_html.pyc in forms(self)
    555             try:
    556                 self._forms_genf = CachingGeneratorFunction(
--> 557                     self._forms_factory.forms())
    558             except:  # XXXX define exception!
    559                 self.set_response(self._response)

/usr/local/lib/python2.7/dist-packages/mechanize-0.2.5-py2.7.egg/mechanize/_html.pyc in forms(self)
    235             _urljoin=_rfc3986.urljoin,
    236             _urlparse=_rfc3986.urlsplit,
--> 237             _urlunparse=_rfc3986.urlunsplit,
    238             )
    239         self.global_form = forms[0]

/usr/local/lib/python2.7/dist-packages/mechanize-0.2.5-py2.7.egg/mechanize/_form.pyc in ParseResponseEx(response, select_default, form_parser_class, request_class, entitydefs, encoding, _urljoin, _urlparse, _urlunparse)
    842                         _urljoin=_urljoin,
    843                         _urlparse=_urlparse,
--> 844                         _urlunparse=_urlunparse,
    845                         )
    846 

/usr/local/lib/python2.7/dist-packages/mechanize-0.2.5-py2.7.egg/mechanize/_form.pyc in _ParseFileEx(file, base_uri, select_default, ignore_errors, form_parser_class, request_class, entitydefs, backwards_compat, encoding, _urljoin, _urlparse, _urlunparse)
    979         data = file.read(CHUNK)
    980         try:
--> 981             fp.feed(data)
    982         except ParseError, e:
    983             e.base_uri = base_uri

/usr/local/lib/python2.7/dist-packages/mechanize-0.2.5-py2.7.egg/mechanize/_form.pyc in feed(self, data)
    758             _sgmllib_copy.SGMLParser.feed(self, data)
    759         except _sgmllib_copy.SGMLParseError, exc:
--> 760             raise ParseError(exc)
    761 
    762     def close(self):

ParseError: expected name token at '<!!--end footer-->\r\n'
[75]中的
:%run-i form_filler.py
---------------------------------------------------------------------------
ParseError回溯(上次最近的调用)
/home/blake/Python/form_filler.py in()
19
20
--->21对于br.forms()中的表单:
22打印“表格名称:”,Form.name
23打印表格
/usr/local/lib/python2.7/dist-packages/mechanize-0.2.5-py2.7.egg/mechanize//u mechanize.pyc格式(self)
418如果不是self.viewing_html():
419提升浏览器状态错误(“不查看HTML”)
-->420返回自我工厂表格()
421
422 def全球表格(自我):
/usr/local/lib/python2.7/dist-packages/mechanize-0.2.5-py2.7.egg/mechanize//u html.pyc格式(self)
555试试:
556 self.\u forms\u genf=CachingGeneratorFunction(
-->557 self.\u forms\u factory.forms())
558例外:#XXXX定义例外!
559自我设置响应(自我设置响应)
/usr/local/lib/python2.7/dist-packages/mechanize-0.2.5-py2.7.egg/mechanize//u html.pyc格式(self)
235 _urljoin=_rfc3986.urljoin,
236 _urlparse=_rfc3986.urlplit,
-->237 _urlunplase=_rfc3986.urlunplast,
238             )
239 self.global_form=forms[0]
/ParseResponseEx中的usr/local/lib/python2.7/dist-packages/mechanize-0.2.5-py2.7.egg/mechanize//u form.pyc
842 _urljoin=_urljoin,
843 _urlparse=_urlparse,
-->844 urlunparse=urlunparse,
845                         )
846
/usr/local/lib/python2.7/dist-packages/mechanize-0.2.5-py2.7.egg/mechanize//u form.pyc in\u ParseFileEx(文件、基本uri、选择默认值、忽略错误、表单解析器类、请求类、entitydefs、向后兼容、编码、\u urljoin、\u urlparse、\u urlunparse)
979 data=file.read(块)
980尝试:
-->981 fp.feed(数据)
982除语法错误外,e:
983 e.base_uri=base_uri
/usr/local/lib/python2.7/dist-packages/mechanize-0.2.5-py2.7.egg/mechanize//u form.pyc in feed(self,data)
758_sgmllib_copy.SGMLParser.feed(self,data)
759除_sgmllib_copy.SGMLParseError外,exc:
-->760上升解析错误(exc)
761
762 def关闭(自):
ParseError:应在'\r\n'处使用名称标记
我的理解是,html在某种程度上“写得很糟糕”。我在谷歌等示例网站上尝试了上述代码,运行良好。我怀疑回车与此有关,但如何绕过此问题?

显然,如果我使用

br = mechanize.Browser(factory=mechanize.RobustFactory()) 
而不是

br = mechanize.Browser()
它给出了正确的输出:

In [18]: % run -i form_filler.py
Form name: qSearchForm
<qSearchForm GET xxx
  <TextControl(q=Search)>
  <SubmitControl(qSearchBtn=) (readonly)>>
Form name: form1
<form1 POST xxx
  <TextControl(name=)>
  <SelectControl(code=[*LER, Lerwick, Scotland, 29.9 / 358.8, ESK, Eskdalemuir, Scotland, 34.7 / 356.8, HAD, Hartland, England, 39.0 / 355.5, ABN, Abinger, England, 38.8 / 359.6, GRW, Greenwich, England, 38.5 / 0.0])>
  <TextControl(month=)>
  <TextControl(year=)>
  <SubmitControl(<None>=Submit Query) (readonly)>
  <IgnoreControl(<None>=<None>)>>
[18]中的
:%run-i form_filler.py
表格名称:qSearchForm
表格名称:表格1