从YouTube视频中获取标题

web*_*kul 29 youtube

我想提取YouTube视频的标题.我怎样才能做到这一点?

谢谢.

Cru*_*uel 59

获取有关youtube视频afaik信息的最简单方法是解析从以下网址检索到的字符串:http://youtube.com/get_video_info?video_id = XXXXXXXX

使用类似PHP的parse_str()之类的东西,你可以获得几乎所有关于视频的数组:

$content = file_get_contents("http://youtube.com/get_video_info?video_id=".$id);
parse_str($content, $ytarr);
echo $ytarr['title'];
Run Code Online (Sandbox Code Playgroud)

这将使用$ id作为视频ID来打印视频的标题.

  • 这已经过时了吗?该网址现在给出 410,已移动或已删除。 (2认同)

小智 9

你好在python3中我建立了两种方式

1) 没有 API KEY

import urllib.request
import json
import urllib
import pprint

#change to yours VideoID or change url inparams
VideoID = "SZj6rAYkYOg" 

params = {"format": "json", "url": "https://www.youtube.com/watch?v=%s" % VideoID}
url = "https://www.youtube.com/oembed"
query_string = urllib.parse.urlencode(params)
url = url + "?" + query_string

with urllib.request.urlopen(url) as response:
    response_text = response.read()
    data = json.loads(response_text.decode())
    pprint.pprint(data)
    print(data['title'])
Run Code Online (Sandbox Code Playgroud)

结果示例:

{'author_name': 'Google Developers',
 'author_url': 'https://www.youtube.com/user/GoogleDevelopers',
 'height': 270,
 'html': '<iframe width="480" height="270" '
         'src="https://www.youtube.com/embed/SZj6rAYkYOg?feature=oembed" '
         'frameborder="0" allow="autoplay; encrypted-media" '
         'allowfullscreen></iframe>',
 'provider_name': 'YouTube',
 'provider_url': 'https://www.youtube.com/',
 'thumbnail_height': 360,
 'thumbnail_url': 'https://i.ytimg.com/vi/SZj6rAYkYOg/hqdefault.jpg',
 'thumbnail_width': 480,
 'title': 'Google I/O 101:  Google APIs: Getting Started Quickly',
 'type': 'video',
 'version': '1.0',
 'width': 480}
Google I/O 101:  Google APIs: Getting Started Quickly
Run Code Online (Sandbox Code Playgroud)

2) 使用 Google API - 必需的 APIKEY

import urllib.request
import json
import urllib
import pprint

APIKEY = "YOUR_GOOGLE_APIKEY"
VideoID = "YOUR_VIDEO_ID"

params = {'id': VideoID, 'key': APIKEY,
          'fields': 'items(id,snippet(channelId,title,categoryId),statistics)',
          'part': 'snippet,statistics'}

url = 'https://www.googleapis.com/youtube/v3/videos'

query_string = urllib.parse.urlencode(params)
url = url + "?" + query_string

with urllib.request.urlopen(url) as response:
    response_text = response.read()
    data = json.loads(response_text.decode())
    pprint.pprint(data)
    print("TITLE: %s " % data['items'][0]['snippet']['title'])
Run Code Online (Sandbox Code Playgroud)

结果示例:

{'items': [{'id': 'SZj6rAYkYOg',
            'snippet': {'categoryId': '28',
                        'channelId': 'UC_x5XG1OV2P6uZZ5FSM9Ttw',
                        'title': 'Google I/O 101:  Google APIs: Getting '
                                 'Started Quickly'},
            'statistics': {'commentCount': '36',
                           'dislikeCount': '20',
                           'favoriteCount': '0',
                           'likeCount': '418',
                           'viewCount': '65783'}}]}
TITLE: Google I/O 101:  Google APIs: Getting Started Quickly
Run Code Online (Sandbox Code Playgroud)


Ale*_*ard 8

这样做的一个方法是获取来自YouTube的视频如图所示在这里

然后从youtube发送的原子提取中提取标题.此处显示示例Feed


sir*_*sko 7

使用JavaScript数据API:

var loadInfo = function (videoId) {
    var gdata = document.createElement("script");
    gdata.src = "http://gdata.youtube.com/feeds/api/videos/" + videoId + "?v=2&alt=jsonc&callback=storeInfo";
    var body = document.getElementsByTagName("body")[0];
    body.appendChild(gdata);
};

var storeInfo = function (info) {
    console.log(info.data.title);
};
Run Code Online (Sandbox Code Playgroud)

然后你只需要打电话loadInfo(videoId).

API文档中提供了更多信息.


abr*_*tez 5

我相信最好的方法是使用youTube的gdata,然后从返回的XML中获取信息

http://gdata.youtube.com/feeds/api/videos/6_Ukfpsb8RI

更新:现在有一个更新的API,您应该使用它

https://developers.google.com/youtube/v3/getting-started

URL: https://www.googleapis.com/youtube/v3/videos?id=7lCDEYXw3mM&key=YOUR_API_KEY
     &fields=items(id,snippet(channelId,title,categoryId),statistics)&part=snippet,statistics

Description: This example modifies the fields parameter from example 3 so that in the API response, each video resource's snippet object only includes the channelId, title, and categoryId properties.

API response:

{
 "videos": [
  {
   "id": "7lCDEYXw3mM",
   "snippet": {
    "channelId": "UC_x5XG1OV2P6uZZ5FSM9Ttw",
    "title": "Google I/O 101: Q&A On Using Google APIs",
    "categoryId": "28"
   },
   "statistics": {
    "viewCount": "3057",
    "likeCount": "25",
    "dislikeCount": "0",
    "favoriteCount": "17",
    "commentCount": "12"
   }
  }
 ]
}
Run Code Online (Sandbox Code Playgroud)


t.y*_*t.y 5

我将按照YouTube API v3 文档 中概述的方式布置流程。

  1. 使用 /登录您希望与您的 YouTube API 使用相关联的 Google 帐户
  2. https://console.developers.google.com/apis/credentials创建一个新项目

    • 在左上角的 Google API 徽标旁边,转到Select a projectCreate project +
    • 等待创建完成。
  3. 制作一个新的 API 密钥。您需要它来访问 v3 下的视频信息。

    • 如果您还没有在那里,请转到左侧导航器下的凭据,API 和服务 > 凭据
    • 在 Credentials 选项卡下,单击Create Credentials并选择API key
    • 将 API 密钥复制到剪贴板。
  4. 提供视频 ID 和您新创建的 API 密钥,转到此链接以查看您的工作:(https://www.googleapis.com/youtube/v3/videos?id=<YOUR VIDEO ID HERE>&key=<YOUR API KEY HERE>%20&part=snippet无尖括号)

例子

URL是,也可以去,哪些URL通过浏览器来检查出来。作为回报,您应该得到API response:.

URL: https://www.googleapis.com/youtube/v3/videos?id=7lCDEYXw3mM&key=YOUR_API_KEY
     &fields=items(id,snippet(channelId,title,categoryId),statistics)&part=snippet,statistics

Description: This example modifies the fields parameter from example 3
             so that in the API response, each video resource's snippet
             object only includes the channelId, title,
             and categoryId properties.

API response:

{
 "videos": [
  {
   "id": "7lCDEYXw3mM",
   "snippet": {
    "channelId": "UC_x5XG1OV2P6uZZ5FSM9Ttw",
    "title": "Google I/O 101: Q&A On Using Google APIs",
    "categoryId": "28"
   },
   "statistics": {
    "viewCount": "3057",
    "likeCount": "25",
    "dislikeCount": "0",
    "favoriteCount": "17",
    "commentCount": "12"
   }
  }
 ]
}
Run Code Online (Sandbox Code Playgroud)

这为您提供.json文件格式的视频信息。如果您的项目要通过 JavaScript 访问此信息,您接下来可能会访问这里:如何在 Javascript 中从 URL 获取 JSON?.


归档时间:

查看次数:

71576 次

最近记录:

6 年,11 月 前