urllib3报错问题解决方法

 时间:2026-02-12 05:39:42

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报错问题解决方法

2、结果就报错了:

AttributeError: module 'urllib3' has no attribute 'urlopen'AttributeError: module 'urllib3' has no attribute 'URLError'

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报错问题解决方法

4、运行结果如下:完美解决问题

urllib3报错问题解决方法

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

  • 塔防之绿野仙踪秘籍全攻略:[5]第五关
  • 阿波尼克号31关攻略
  • 保卫萝卜4第二章第4关攻略
  • 保卫萝卜4阿波尼克号18关攻略
  • 保卫萝卜4绿野奇缘24关攻略
  • 热门搜索
    起亚k3怎么样 怎么申请退款 睡觉磨牙是怎么回事 晚上尿多怎么回事 左后背疼是怎么回事 妈妈是怎么堕落的 疏怎么组词 新鲜石斛怎么吃 下巴长痘是怎么回事 手机淘宝怎么收藏店铺