我試圖編寫我自己的Python代碼,使用youtubeinmp3 API將多個youtube視頻下載爲mp3文件。它對大多數文件都很好,但有些是46Kb HTML頁面而不是mp3。有什麼辦法可以讓我的代碼自己解決這個問題嗎?youtubeinmp3的api直接鏈接返回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
[編輯]你的問題,並顯示這些_HTML pages_如何破壞你的代碼? – stovfl