【发布时间】:2017-08-09 09:20:25
【问题描述】:
我尝试编写自己的 Python 代码,以使用 youtubeinmp3 API 将多个 youtube 视频下载为 mp3 文件。它适用于大多数文件,但有些是 46Kb HTML 页面而不是 mp3。有什么办法可以让我的代码自行解决这个问题?
脚本如下:
import urllib, json, re
#List of Youtube Video IDs to download
ListofURL = open("list.txt","r")
Ids = [url.strip().replace('https://www.youtube.com/watch?v=','') for url in ListofURL.readlines()]
#Base url
url = "http://www.youtubeinmp3.com/fetch/format=JSON&video=https://www.youtube.com/watch?v="
for id in Ids:
#Getting the API's download link as json response
response = urllib.urlopen(url+id)
data = json.loads("{" + re.findall('\{(.*?)\}', response.read())[0] + "}")
#Creating a file to download the song to
target = open(data["title"]+".mp3", 'wb')
#Downloading the mp3
mresponse = urllib.urlopen(data["link"])
mdata = mresponse.read()
#saving Data to the created file
target.write(mdata)
#closing the created file
target.close()
脚本以这种格式从文件中读取 youtube 地址:
https://www.youtube.com/watch?v=uzpa6ACrZaQ
https://www.youtube.com/watch?v=_B_3g_9gtFQ
https://www.youtube.com/watch?v=ex0Hli7kMRs
【问题讨论】:
-
Edit您的问题并展示这些_HTML页面_如何破坏您的代码?