【问题标题】:youtubeinmp3's api direct link returns html page instead of mp3 fileyoutubeinmp3 的 api 直接链接返回 html 页面而不是 mp3 文件
【发布时间】: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页面_如何破坏您的代码?

标签: python html json api mp3


【解决方案1】:

没有办法...我自己尝试过这样做。有些视频被阻止了,有些视频不在他们的数据库中,所以他们不会下载,而 download.htm 取代了他们的位置,这是 youtubeinmp3 页面,它只是重定向到自身。我认为这是由网站管理员尝试传播他们的广告和看起来像恶意软件的广告.. 不好,但有什么办法...

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

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

查看网站http://youtubemp3.today/,看看它是如何工作的,并从 1 (youtubeinmp3) 切换到另一个..

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-08-27
    • 2019-02-04
    • 2018-11-18
    • 2020-05-05
    • 2017-04-12
    • 2020-01-09
    • 2021-08-19
    相关资源
    最近更新 更多