Python mechanize连接失败问题
我正在运行一个爬虫程序,它在MAC和Ubuntu系统中运行良好。 在windows中运行时,会产生以下堆栈跟踪:Python mechanize连接失败问题,python,mechanize,web-crawler,python-requests,Python,Mechanize,Web Crawler,Python Requests,我正在运行一个爬虫程序,它在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
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>
文件“build\bdist.win32\egg\mechanize\\u mechanize.py”,第203行,打开
返回self.\u mech\u open(url、数据、超时=超时)
文件“build\bdist.win32\egg\mechanize\\u mechanize.py”,第230行,处于“打开”状态
response=UserAgentBase.open(self、request、data)
文件“build\bdist.win32\egg\mechanize\\u opener.py”,第193行,打开
响应=urlopen(自我、请求、数据)
文件“build\bdist.win32\egg\mechanize\\u urlib2\u fork.py”,第344行,处于打开状态
"开放",
文件“build\bdist.win32\egg\mechanize\\u urlib2\u fork.py”,第332行,在调用链中
结果=func(*args)
文件“build\bdist.win32\egg\mechanize\\u urllib2\u fork.py”,第1170行,https\u open
返回自动打开(连接工厂,需要)
文件“build\bdist.win32\egg\mechanize\\u urlib2\u fork.py”,第1118行,在do\u open中
引发URL错误(err)
URL错误:
我非常怀疑问题是否与您的系统有关。要解决此问题,我建议:
set\u handle\u robots
和addheaders
。问题可能与每个IP连接的请求太多。(我怀疑这才是真正的问题。)如果上述解决方案均无效,请提供导致错误的代码。根据错误,我推断您尝试连接的服务器不允许您的连接 尝试使用
telnet
再次检查您是否可以连接到您请求的URL/IP:PORT
:
telnet <ip> <port>
telnet
PS:以防万一,在检查连接时不要使用ping,因为您没有测试端口。是特定的URL导致它抛出此错误吗<代码>URL错误看起来像是超时错误。使用
try..except
来处理此错误,如果它不是致命错误。请将发出请求的部分代码放在此处。您是否在所有系统上使用相同的python和mechanize版本?如果您发布一个完整的代码片段,一致地再现问题,您可能会更幸运。我和其他评论者一样,我猜在Mechanize之外还有其他一些不可靠的事情。诚然,Windows分叉与*nix系统不同,这可能是其中的一部分,但几乎可以肯定的是,这并不是整个谜团。