【问题】
写Python代码,利用urllib2去访问网络,结果期间会出现错误:urllib2.URLError:
【解决过程】
1.后来发现,程序本身是好的,但是是由于,网络的偶尔的不稳定,而导致了此错误的。
2.所以后来就想到,当发现网络遇到这类错误的时候,多试几次,应该就可以解决此问题了。
所以把原先的:#itemRespHtml = crifanLib.getUrlRespHtml(itemLink);
改为:itemRespHtml = crifanLib.getUrlRespHtml_multiTry(itemLink);
其中对应的代码是:
中的:#------------------------------------------------------------------------------
def getUrlResponse(url, postDict={}, headerDict={}, timeout=0, useGzip=False, postDataDelimiter="&") :
"""Get response from url, support optional postDict,headerDict,timeout,useGzip
Note:
1. if postDict not null, url request auto become to POST instead of default GET
2 if you want to auto handle cookies, should call initAutoHandleC