urllib2.urlopen(url,timeout=10).read() 循环采集 如果一超时后报错 然后就停止运行了
请问一下如果超时了还让程序继续运行下去!
Traceback (most recent call last):
File "E:\PYTHON EXAMPLE\robot.py", line 114, in <module>
runTask(work, day=0, hour=0, min=0, second=5)
File "E:\PYTHON EXAMPLE\robot.py", line 104, in runTask
func()
File "E:\PYTHON EXAMPLE\robot.py", line 55, in work
bk_browse = urllib2.urlopen(url,timeout=10).read()
File "C:\Python27\lib\urllib2.py", line 154, in urlopen
return opener.open(url, data, timeout)
File "C:\Python27\lib\urllib2.py", line 431, in open
response = self._open(req, data)
File "C:\Python27\lib\urllib2.py", line 449, in _open
'_open', req)
File "C:\Python27\lib\urllib2.py", line 409, in _call_chain
result = func(*args)
File "C:\Python27\lib\urllib2.py", line 1227, in http_open
return self.do_open(httplib.HTTPConnection, req)
File "C:\Python27\lib\urllib2.py", line 1200, in do_open
r = h.getresponse(buffering=True)
File "C:\Python27\lib\httplib.py", line 1074, in getresponse
response.begin()
File "C:\Python27\lib\httplib.py", line 415, in begin
version, status, reason = self._read_status()
File "C:\Python27\lib\httplib.py", line 371, in _read_status
line = self.fp.readline(_MAXLINE + 1)
File "C:\Python27\lib\socket.py", line 476, in readline
data = self._sock.recv(self._rbufsize)
error: [Errno 10054]
1
aec4d 2015-03-06 16:19:50 +08:00
import requests
requests.packages.urllib3.disable_warnings = True requests.get(url, timeout=10).text |
3
xierch 2015-03-06 16:33:15 +08:00
try ... except ...
|
4
Sylv 2015-03-06 16:35:03 +08:00
import socket
try: >>> urllib2.urlopen(url,timeout=10).read() except socket.error: >>> continue PS: Errno 10054 并不是超时,而是连接被远端重置。 |
6
Sylv 2015-03-06 17:04:27 +08:00 via iPhone
@156007766 重置指的是:你请求太频繁了,超出人家服务器的限制了,然后远程服务器端就把你的连接强制关闭了。
解决办法是降低请求频率,或者用多个代理。 |
8
156007766 OP |
9
gamexg 2015-03-07 21:49:31 +08:00
伪装 headers 了吗?
|
10
program2000 2015-03-09 10:40:13 +08:00
要用try except拦截,不然这个错误解决了,下次还会有各种其他错误。。。。
|
11
156007766 OP |