设置全局超时
最后,抓取时如果实在找不到什么函数能设置超时时间,那么可以设置全局的 socket 超时,虽然这样做不大合适:
>>> import socket
>>> socket.setdefaulttimeout(90)
setdefaulttimeout() was a hack to allow to set the timeout when nothing else is available. 如何捕获超时异常?
举例: from urllib2 import urlopen
import socket
slowurl =”http://www.wenxuecity.com/”
socket.setdefaulttimeout(1)
try:
data = urlopen(slowurl)
data.read() except socket.error:
errno, errstr = sys.exc_info()[:2]
if errno == socket.timeout:
print "There was a timeout"
else:
print "There was some other socket error"
参考资源:
1、 No way to disable socket timeouts in httplib, etc.
2、How to catch socket timeout?