【问题标题】:Downloading a .mp3 file in python [duplicate]在python中下载.mp3文件[重复]
【发布时间】:2013-12-29 00:41:10
【问题描述】:

我正在尝试下载一个 .mp3 文件,但是,当我下载它时,它已被下载,但听起来像是通过我的扬声器发出的吱吱声和其他奇怪的噪音(类似于当您的 .mp3 文件在编码中有错误时) )。

这是我当前的代码:

# sets the song url to mp3link variable.
mp3link = dLink[0]

# opens the .mp3 file - using the same procedure as above.
openmp3 = open('testing.mp3', 'w')
dl = urllib2.urlopen(mp3link)
dl2 = dl.read()

# writes the .mp3 file to the file 'testing.mp3' which is in the variable openmp3.
openmp3.write(dl2)
openmp3.close()

print 'done'

我知道我可以将此代码用作更快的方法:

dlmp3 = urllib2.urlopen(url)
with open('testing2.mp3', 'wb') as filee:
    filee.write(dlmp3.read())

有没有人可以告诉我我做错了什么以及如何解决它? 谢谢。

【问题讨论】:

    标签: python mp3 download


    【解决方案1】:

    我找到了解决方法:Downloading Mp3 using Python in Windows mangles the song however in Linux it doesn't

    "尝试二进制文件模式。open(mp3Name, "wb") 你可能会得到行尾翻译。

    文件是二进制的,是的。这是没有的模式。打开文件时,可以将其设置为作为文本文件读取(这是默认设置)。当它这样做时,它将转换行尾以匹配平台。在 Windows 上,行尾是 \r\n 在大多数其他地方它不是 \r 或 \n。这种变化弄乱了数据流。 "

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-12-09
      • 1970-01-01
      • 2014-03-04
      • 1970-01-01
      • 2017-07-04
      • 2011-12-29
      • 2013-01-09
      • 2014-08-06
      相关资源
      最近更新 更多