Python3.7中urllib.urlopen 报错问题

博客记录了Python代码运行报错信息,显示'AttributeError: module 'urllib' has no attribute 'urlopen''。原因是Python3里urllib模块改变,解决方案是将urllib改成urllib.request,更改代码后运行成功。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import urllib
web = urllib.urlopen('https://www.baidu.com')
f = web.read()
print(f)

报错:

Traceback (most recent call last):
  File "D:/coding/python/text.py", line 2, in <module>
    web = urllib.urlopen('https://www.baidu.com')
AttributeError: module 'urllib' has no attribute 'urlopen'

Process finished with exit code 1


解决方案:Python3里的urllib模块已经发生改变,此处的urllib都应该改成urllib.request。

 

更改后的代码

import urllib.request
web = urllib.request.urlopen('https://www.baidu.com')
f = web.read()
print(f)

结果显示:

 

 运行成功

Arguments: datasets_root: None models_dir: saved_models cpu: False seed: None Exception in thread Thread-2: Traceback (most recent call last): File "/home/human588/.conda/envs/audio_env/lib/python3.7/urllib/request.py", line 1350, in do_open encode_chunked=req.has_header(&#39;Transfer-encoding&#39;)) File "/home/human588/.conda/envs/audio_env/lib/python3.7/http/client.py", line 1281, in request self._send_request(method, url, body, headers, encode_chunked) File "/home/human588/.conda/envs/audio_env/lib/python3.7/http/client.py", line 1327, in _send_request self.endheaders(body, encode_chunked=encode_chunked) File "/home/human588/.conda/envs/audio_env/lib/python3.7/http/client.py", line 1276, in endheaders self._send_output(message_body, encode_chunked=encode_chunked) File "/home/human588/.conda/envs/audio_env/lib/python3.7/http/client.py", line 1036, in _send_output self.send(msg) File "/home/human588/.conda/envs/audio_env/lib/python3.7/http/client.py", line 976, in send self.connect() File "/home/human588/.conda/envs/audio_env/lib/python3.7/http/client.py", line 1443, in connect super().connect() File "/home/human588/.conda/envs/audio_env/lib/python3.7/http/client.py", line 948, in connect (self.host,self.port), self.timeout, self.source_address) File "/home/human588/.conda/envs/audio_env/lib/python3.7/socket.py", line 728, in create_connection raise err File "/home/human588/.conda/envs/audio_env/lib/python3.7/socket.py", line 716, in create_connection sock.connect(sa) OSError: [Errno 101] Network is unreachable During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/home/human588/.conda/envs/audio_env/lib/python3.7/threading.py", line 926, in _bootstrap_inner self.run() File "/home/human588/.conda/envs/audio_env/lib/python3.7/threading.py", line 870, in run self._target(*self._args, **self._kwargs) File "/python/Real-Time-Voice-Cloning-master/utils/default_models.py", line 30, in download urllib.request.urlretrieve(url, filename=target, reporthook=t.update_to) File "/home/human588/.conda/envs/audio_env/lib/python3.7/urllib/request.py", line 247, in urlretrieve with contextlib.closing(urlopen(url, data)) as fp: File "/home/human588/.conda/envs/audio_env/lib/python3.7/urllib/request.py", line 222, in urlopen return opener.open(url, data, timeout) File "/home/human588/.conda/envs/audio_env/lib/python3.7/urllib/request.py", line 525, in open response = self._open(req, data) File "/home/human588/.conda/envs/audio_env/lib/python3.7/urllib/request.py", line 543, in _open &#39;_open&#39;, req) File "/home/human588/.conda/envs/audio_env/lib/python3.7/urllib/request.py", line 503, in _call_chain result = func(*args) File "/home/human588/.conda/envs/audio_env/lib/python3.7/urllib/request.py", line 1393, in https_open context=self._context, check_hostname=self._check_hostname) File "/home/human588/.conda/envs/audio_env/lib/python3.7/urllib/request.py", line 1352, in do_open raise URLError(err) urllib.error.URLError: <urlopen error [Errno 101] Network is unreachable> Exception in thread Thread-1: Traceback (most recent call last): File "/home/human588/.conda/envs/audio_env/lib/python3.7/urllib/request.py", line 1350, in do_open encode_chunked=req.has_header(&#39;Transfer-encoding&#39;)) File "/home/human588/.conda/envs/audio_env/lib/python3.7/http/client.py", line 1281, in request self._send_request(method, url, body, headers, encode_chunked) File "/home/human588/.conda/envs/audio_env/lib/python3.7/http/client.py", line 1327, in _send_request self.endheaders(body, encode_chunked=encode_chunked) File "/home/human588/.conda/envs/audio_env/lib/python3.7/http/client.py", line 1276, in endheaders self._send_output(message_body, encode_chunked=encode_chunked) File "/home/human588/.conda/envs/audio_env/lib/python3.7/http/client.py", line 1036, in _send_output self.send(msg) File "/home/human588/.conda/envs/audio_env/lib/python3.7/http/client.py", line 976, in send self.connect() File "/home/human588/.conda/envs/audio_env/lib/python3.7/http/client.py", line 1443, in connect super().connect() File "/home/human588/.conda/envs/audio_env/lib/python3.7/http/client.py", line 948, in connect (self.host,self.port), self.timeout, self.source_address) File "/home/human588/.conda/envs/audio_env/lib/python3.7/socket.py", line 728, in create_connection raise err File "/home/human588/.conda/envs/audio_env/lib/python3.7/socket.py", line 716, in create_connection sock.connect(sa) OSError: [Errno 101] Network is unreachable During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/home/human588/.conda/envs/audio_env/lib/python3.7/threading.py", line 926, in _bootstrap_inner self.run() File "/home/human588/.conda/envs/audio_env/lib/python3.7/threading.py", line 870, in run self._target(*self._args, **self._kwargs) File "/python/Real-Time-Voice-Cloning-master/utils/default_models.py", line 30, in download urllib.request.urlretrieve(url, filename=target, reporthook=t.update_to) File "/home/human588/.conda/envs/audio_env/lib/python3.7/urllib/request.py", line 247, in urlretrieve with contextlib.closing(urlopen(url, data)) as fp: File "/home/human588/.conda/envs/audio_env/lib/python3.7/urllib/request.py", line 222, in urlopen return opener.open(url, data, timeout) File "/home/human588/.conda/envs/audio_env/lib/python3.7/urllib/request.py", line 525, in open response = self._open(req, data) File "/home/human588/.conda/envs/audio_env/lib/python3.7/urllib/request.py", line 543, in _open &#39;_open&#39;, req) File "/home/human588/.conda/envs/audio_env/lib/python3.7/urllib/request.py", line 503, in _call_chain result = func(*args) File "/home/human588/.conda/envs/audio_env/lib/python3.7/urllib/request.py", line 1393, in https_open context=self._context, check_hostname=self._check_hostname) File "/home/human588/.conda/envs/audio_env/lib/python3.7/urllib/request.py", line 1352, in do_open raise URLError(err) urllib.error.URLError: <urlopen error [Errno 101] Network is unreachable> Exception in thread Thread-4: Traceback (most recent call last): File "/home/human588/.conda/envs/audio_env/lib/python3.7/urllib/request.py", line 1350, in do_open encode_chunked=req.has_header(&#39;Transfer-encoding&#39;)) File "/home/human588/.conda/envs/audio_env/lib/python3.7/http/client.py", line 1281, in request self._send_request(method, url, body, headers, encode_chunked) File "/home/human588/.conda/envs/audio_env/lib/python3.7/http/client.py", line 1327, in _send_request self.endheaders(body, encode_chunked=encode_chunked) File "/home/human588/.conda/envs/audio_env/lib/python3.7/http/client.py", line 1276, in endheaders self._send_output(message_body, encode_chunked=encode_chunked) File "/home/human588/.conda/envs/audio_env/lib/python3.7/http/client.py", line 1036, in _send_output self.send(msg) File "/home/human588/.conda/envs/audio_env/lib/python3.7/http/client.py", line 976, in send self.connect() File "/home/human588/.conda/envs/audio_env/lib/python3.7/http/client.py", line 1443, in connect super().connect() File "/home/human588/.conda/envs/audio_env/lib/python3.7/http/client.py", line 948, in connect (self.host,self.port), self.timeout, self.source_address) File "/home/human588/.conda/envs/audio_env/lib/python3.7/socket.py", line 728, in create_connection raise err File "/home/human588/.conda/envs/audio_env/lib/python3.7/socket.py", line 716, in create_connection sock.connect(sa) OSError: [Errno 101] Network is unreachable During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/home/human588/.conda/envs/audio_env/lib/python3.7/threading.py", line 926, in _bootstrap_inner self.run() File "/home/human588/.conda/envs/audio_env/lib/python3.7/threading.py", line 870, in run self._target(*self._args, **self._kwargs) File "/python/Real-Time-Voice-Cloning-master/utils/default_models.py", line 30, in download urllib.request.urlretrieve(url, filename=target, reporthook=t.update_to) File "/home/human588/.conda/envs/audio_env/lib/python3.7/urllib/request.py", line 247, in urlretrieve with contextlib.closing(urlopen(url, data)) as fp: File "/home/human588/.conda/envs/audio_env/lib/python3.7/urllib/request.py", line 222, in urlopen return opener.open(url, data, timeout) File "/home/human588/.conda/envs/audio_env/lib/python3.7/urllib/request.py", line 525, in open response = self._open(req, data) File "/home/human588/.conda/envs/audio_env/lib/python3.7/urllib/request.py", line 543, in _open &#39;_open&#39;, req) File "/home/human588/.conda/envs/audio_env/lib/python3.7/urllib/request.py", line 503, in _call_chain result = func(*args) File "/home/human588/.conda/envs/audio_env/lib/python3.7/urllib/request.py", line 1393, in https_open context=self._context, check_hostname=self._check_hostname) File "/home/human588/.conda/envs/audio_env/lib/python3.7/urllib/request.py", line 1352, in do_open raise URLError(err) urllib.error.URLError: <urlopen error [Errno 101] Network is unreachable> Traceback (most recent call last): File "demo_toolbox.py", line 34, in <module> ensure_default_models(args.models_dir) File "/python/Real-Time-Voice-Cloning-master/utils/default_models.py", line 55, in ensure_default_models f"Download for {target_path.name} failed. You may download models manually instead.\n" \ AssertionError: Download for encoder.pt failed. You may download models manually instead. https://drive.google.com/drive/folders/1fU6umc5uQAVR2udZdHX-lDgXYzTyqG_j
最新发布
07-21
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值