【问题标题】:Python mechanize connection failed issuePython机械化连接失败问题
【发布时间】:2012-05-24 18:08:21
【问题描述】:

我正在运行一个爬虫,它在 MAC 和 Ubuntu 系统中运行良好。 当我在 Windows 中运行它时,它会导致以下堆栈跟踪:

  File "build\bdist.win32\egg\mechanize\_mechanize.py", line 203, in open
    return self._mech_open(url, data, timeout=timeout)
  File "build\bdist.win32\egg\mechanize\_mechanize.py", line 230, in _mech_open
    response = UserAgentBase.open(self, request, data)
  File "build\bdist.win32\egg\mechanize\_opener.py", line 193, in open
    response = urlopen(self, req, data)
  File "build\bdist.win32\egg\mechanize\_urllib2_fork.py", line 344, in _open
    '_open', req)
  File "build\bdist.win32\egg\mechanize\_urllib2_fork.py", line 332, in _call_chain
    result = func(*args)
  File "build\bdist.win32\egg\mechanize\_urllib2_fork.py", line 1170, in https_open
    return self.do_open(conn_factory, req)
  File "build\bdist.win32\egg\mechanize\_urllib2_fork.py", line 1118, in do_open
    raise URLError(err)
URLError: <urlopen error [Errno 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond>

【问题讨论】:

  • 是一个特定的 URL 导致它抛出这个错误吗? URLError 看起来像超时错误。如果结果不是致命的,请使用try..except 处理此错误。
  • 将发出请求的代码部分放在这里。
  • 您是否在所有系统上使用相同的 python 和 mechanize 版本?
  • 如果您发布一个完整的代码 sn-p 可以持续重现问题,您可能会更幸运。我和其他评论者在一起,因为我猜在 Mechanize 之外还有其他一些狡猾的事情发生。诚然,Windows fork 与 *nix 系统不同,也许这是其中的一部分,但几乎可以肯定这不是整个难题。

标签: python mechanize web-crawler python-requests


【解决方案1】:

我非常怀疑问题是否与您的系统有关。为了解决这个问题,我建议:

  1. 检查您的 mechanize 版本。旧版本可能会以不同方式处理请求。
  2. 调查set_handle_robotsaddheaders。问题可能与每个 IP 的请求过多有关。 (我怀疑这是真正的问题。)
  3. 检查错误处理。就像上面评论中描述的那样,无论如何您都应该添加适当的错误处理。

如果上述解决方案均无效,请提供导致错误的代码。

【讨论】:

    【解决方案2】:

    从错误中我推断您尝试连接的服务器不允许您的连接。

    尝试使用telnet 仔细检查您是否可以连接到您请求的URL/IP:PORT

    telnet <ip> <port>
    

    PS:以防万一,不要使用 ping 检查连接性,因为您没有测试端口。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-27
      • 1970-01-01
      • 2011-07-13
      相关资源
      最近更新 更多