AttributeError: 'module' 对象没有属性 'urlretrieve' [英] AttributeError: 'module' object has no attribute 'urlretrieve'

查看:24
本文介绍了AttributeError: 'module' 对象没有属性 'urlretrieve'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to write a program that will download mp3's off of a website then join them together but whenever I try to download the files I get this error:

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 模块.它已被拆分为多个模块.

As you're using Python 3, there is no urllib module anymore. It has been split into several modules.

这相当于urlretrieve:

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

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

urlretrieve behaves exactly the same way as it did in Python 2.x, so it'll work just fine.

基本上:

  • urlretrieve 将文件保存到一个临时文件并返回一个元组 (filename, headers)
  • urlopen 返回一个 Request 对象,其 read 方法返回一个包含文件内容的字节串
  • urlretrieve saves the file to a temporary file and returns a tuple (filename, headers)
  • urlopen returns a Request object whose read method returns a bytestring containing the file contents

这篇关于AttributeError: 'module' 对象没有属性 'urlretrieve'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆