网站首页 美食营养 游戏数码 手工爱好 生活家居 健康养生 运动户外 职场理财 情感交际 母婴教育 时尚美容

urllib3报错问题解决方法

时间:2024-10-12 01:41:46

1、原始代码如下:import urllib3def download(url,num_retries=2): print('Downloading:',url) try: html = urllib3.urlopen(url).read() except urllib3.URLError as e: print('Downloading error:',e.reason) html = None if num_retries > 0: if hasattr(e,'code') and 500<=e.code<600: #recursively retry 5xx HTTP errors return download(url,num_retries-1) return htmldownload('http://httpstat.us/500')(注释:这段代码在urllib2中使用是没有问题的,换成urllib3使用报错如下:)

urllib3报错问题解决方法

3、原因是:urllib3将所有的urllib都打包为urllib所以代码应修改为:urllib.request修改后的代码如下:import urllib.requestdef download(url,num_retries=2): print('Downloading:',url) try: html = urllib.request.urlopen(url).read() except urllib.request.URLError as e: print('Downloading error:',e.reason) html = None if num_retries > 0: if hasattr(e,'code') and 500<=e.code<600: #recursively retry 5xx HTTP errors return download(url,num_retries-1) return htmldownload('http://httpstat.us/500')

urllib3报错问题解决方法

5、如果对你有用话给过投个票呗!!!

© 2025 小知经验
信息来自网络 所有数据仅供参考
有疑问请联系站长 site.kefu@gmail.com