python-3.x - AttributeError: 'module' 对象没有属性 'urlretrieve'

标签 python-3.x urllib attributeerror

我正在尝试编写一个程序,从网站下载 mp3,然后将它们连接在一起,但每当我尝试下载文件时,我都会收到此错误:

Traceback (most recent call last):
File "/home/tesla/PycharmProjects/OldSpice/Voicemail.py", line 214, in <module> main()
File "/home/tesla/PycharmProjects/OldSpice/Voicemail.py", line 209, in main getMp3s()
File "/home/tesla/PycharmProjects/OldSpice/Voicemail.py", line 134, in getMp3s
raw_mp3.add = urllib.urlretrieve("http://www-scf.usc.edu/~chiso/oldspice/m-b1-hello.mp3")
AttributeError: 'module' object has no attribute 'urlretrieve'

导致此问题的行是

raw_mp3.add = urllib.urlretrieve("http://www-scf.usc.edu/~chiso/oldspice/m-b1-hello.mp3")

最佳答案

由于您使用的是 Python 3,因此不再有 urllib 模块。它已被分成几个模块。

这相当于urlretrieve:

import urllib.request
data = urllib.request.urlretrieve("http://...")

urlretrieve 的行为方式与 Python 2.x 中的行为方式完全相同,因此它可以正常工作。

基本上:

  • urlretrieve 将文件保存到临时文件并返回元组 (filename, headers)
  • urlopen 返回一个 Request 对象,其 read 方法返回包含文件内容的字节字符串

关于python-3.x - AttributeError: 'module' 对象没有属性 'urlretrieve',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17960942/

相关文章:

python - AttributeError:模块 'matplotlib' 没有属性 'xlabel'

python - AttributeError - 即使似乎没有属性错误

python - Celery 在使用不正确的 broker url 发布任务时挂起

python - 我正在尝试理解 Python 中的二进制文件模式

python-3.x - Selenium + Firefox开发版: send_keys throwing [object Undefined] exception

python - 使用带斜杠的 python quote_plus

python - 使用\w+ 在正则表达式搜索中包含 '-'。 Python

python - 将多个值存储到 mysql 数据库中的单个字段中以保留 Django 中的顺序

python-3.x - Python3 urllib图像检索

javascript - Python urllib.unquote_plus node.js 等效项