youtubeinmp3的api直接链接返回html页面而不是mp3文件

时间:2017-08-09 09:20:25

标签: python html json api mp3

我尝试编写自己的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

1 个答案:

答案 0 :(得分:0)

没有办法......我自己试着这样做。一些视频被阻止,其他一些视频不在他们的数据库中,因此他们不会下载并且download.htm取代了他们的位置,这就是youtubeinmp3页面,它只是重定向到自身..我认为它是由网站管理员试图广告他们的广告和恶意软件看广告..不好,但可以做什么......

并且没有办法检查。我实际上尝试获取最终下载链接的标题,并使用“wget --spider URL”进行,以便检查ffinal重定向是否解析为mp3或页面。即使它解析为mp3,当我尝试下载它时 - 它仍然有时会恢复到download.htm。

还有其他youtube到mp3 apis。我用不同的api实现了“method2”,它实际上有进度指示器和其他东西..虽然我不得不在那里提取下载链接,因为它也想推出自己的广告......

查看网站http://youtubemp3.today/并查看其工作原理并从1(youtubeinmp3)切换到另一个..

相关问题