【问题标题】:Error in using Python/mechanize select_form()?使用 Python/mechanize select_form() 时出错?
【发布时间】:2011-01-05 06:59:34
【问题描述】:

我正在尝试从网站上抓取一些数据。 我正在尝试编写的脚本应该获取页面的内容:

http://www.atpworldtour.com/Rankings/Singles.aspx

应该模拟用户通过附加排名和日期的每个选项,并模拟点击Go,然后在获取数据后应该使用返回功能。

目前我一直在尝试只为附加声望选择此选项:

            <option value="101" >101-200</option>

这是我尝试这样做的(糟糕的)尝试:

from mechanize import Browser
from BeautifulSoup import BeautifulSoup
import re
import urllib2



br = Browser();
br.open("http://www.atpworldtour.com/Rankings/Singles.aspx");
br.select_form(nr=0);
br["r"] = "101";

response = br.submit();

但是它只是在应该选择第一个表单的 select_form(nr=0) 上失败。

这是 Python 返回的日志:

>>> from mechanize import Browser
>>>
>>> from BeautifulSoup import BeautifulSoup
>>> import re
>>> import urllib2
>>>
>>>
>>>
>>> br = Browser();
>>> br.open("http://www.atpworldtour.com/Rankings/Singles.aspx");
<response_seek_wrapper at 0x311bb48L whose wrapped object = <closeable_response
at 0x311be88L whose fp = <socket._fileobject object at 0x0000000002C94408>>>
>>> br.select_form(nr=0);
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "build\bdist.win-amd64\egg\mechanize\_mechanize.py", line 505, in select_
form
  File "build\bdist.win-amd64\egg\mechanize\_html.py", line 546, in __getattr__
  File "build\bdist.win-amd64\egg\mechanize\_html.py", line 559, in forms
  File "build\bdist.win-amd64\egg\mechanize\_html.py", line 228, in forms
mechanize._html.ParseError

我在 mechanize 主页中找不到所有功能的正确解释。谁能指出我使用表格和机械化的正确教程或帮助我解决这个特定问题?

安东尼

【问题讨论】:

    标签: python mechanize web-scraping


    【解决方案1】:

    我认为您正确使用了该库,但解析器似乎在处理该特定页面时遇到了问题。我在另一个页面(“http://flashcarddb.com/login”)上以相同的方式使用该库,它不会引发错误。

    【讨论】:

      【解决方案2】:

      我刚刚遇到了同样的问题。我访问的页面通过了 W3C 验证,所以我认为这不是标记问题。但是,html tidy 抱怨该页面在 .一旦我解决了这个问题,mechanize 就开始工作了。

      另外,我在邮件列表上看到了对此问题的回复。我只想指出,将 factory=mechanize.RobustFactory() 添加到 mechanize.Browser() 并没有改变结果。

      【讨论】:

        【解决方案3】:

        线索:定义更多关于你的 mechanize.Browser()

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2012-06-17
          • 1970-01-01
          • 2012-05-12
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-01-29
          相关资源
          最近更新 更多