Python/Mechanize - 无法选择表单 - 解析错误(exc)

1 投票
2 回答
1938 浏览
提问于 2025-04-16 06:20

我遇到了这个错误:

>>> br = Browser()
>>> br.open("http://www.bestforumz.com/forum/")
<response_seek_wrapper at 0x21f9fd0
whose wrapped object =
<closeable_response at 0x21f9558 whose
fp = <socket._fileobject object at
0x021F5F30>>>
>>> br.select_form(nr=0)

Traceback (most recent call last):
  File "<pyshell#3>", line 1, in <module>
    br.select_form(nr=0)
  File "build\bdist.win32\egg\mechanize\_mechanize.py", line 505, in select_form
    global_form = self._factory.global_form
  File "build\bdist.win32\egg\mechanize\_html.py", line 546, in __getattr__
    self.forms()
  File "build\bdist.win32\egg\mechanize\_html.py", line 559, in forms
    self._forms_factory.forms())
  File "build\bdist.win32\egg\mechanize\_html.py", line 228, in forms
    raise ParseError(exc)
ParseError: <unprintable ParseError object>

请帮帮我

谢谢

2 个回答

3

我告诉你,这是一种我用来解析HTML的秘密方法(目标是通过mechanize强制解析HTML)。

br = mechanize.Browser(factory=mechanize.DefaultFactory(i_want_broken_xhtml_support=True))
1

mechanize这个库并不能保证能解析所有的HTML内容。有时候你可能需要手动去处理这些内容(不过这也不难,因为我们用的是Python)。

你是在尝试向网站的search.php页面发送查询吗?你可以使用urllib2来实现这个功能。

import urllib2
import urllib

values = dict(foo="hello", bar="world") # examine form for actual vars
try:
    req = urllib2.Request("http://example.com/search.php",
                          urllib.urlencode(values))
    response_page = urllib2.urlopen(req).read()
except urllib2.HTTPError, details:
    pass #do something with the error here...

撰写回答