使用 youtube api v3 从 url 获取 youtube 视频标题和类别 [英] Fetch youtube video title and category from url using youtube api v3

查看:30
本文介绍了使用 youtube api v3 从 url 获取 youtube 视频标题和类别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我完全是初学者!我想从 python 中的 Url 获取 youtube 视频的标题以及它的类别.

I'm a total beginner at this! I want to fetch the title of a youtube video and also it's category from the Url in python.

import lxml
from lxml import etree
youtube = etree.HTML(urllib.urlopen("http://www.youtube.com/watch?v=KQEOBZLx-Z8").read())
video_title = youtube.xpath("//span[@id='eow-title']/@title")
print ''.join(video_title)

输出:'圣诞节的 12 天 - 圣诞颂歌'

Output : '12 Days of Christmas - Christmas Carol'

我已经尝试了 lxml 模块来获取标题,但它不适用于所有视频,我希望使用 youtube api v3.但是我该如何开始,我浏览了很多网站并试图寻求帮助,但我的控制台总是出现错误.我已经在 https://console.developers.google.com 为我的项目创建了服务器密钥,什么我现在应该做什么?我浏览了官方页面,但无法理解.我应该从哪里开始?我还查看了其他几个 SO 问题,这些代码对我不起作用.

I've tried the lxml module to obtain the title but it doesn't work for all videos and I wish to use the youtube api v3. But how do I start, I went through a lot of sites and tried to take help but I always get an error on my console. I have created the server key for my project at https://console.developers.google.com, what should I do now? I went through the official page but couldn't undestand much. Where should I start? I also looked at a couple of other SO questions and the codes didn't work for me.

 import urllib
 import simplejson

 id = 'KQEOBZLx-Z8'
 url = 'http://gdata.youtube.com/feeds/api/videos/%s?alt=json&v=2' % id

 json = simplejson.load(urllib.urlopen(url))

 title = json['entry']['title']['$t']
 author = json['entry']['author'][0]['name']

 print "id:%s\nauthor:%s\ntitle:%s" % (id, author, title)  

我该怎么办?

推荐答案

您可以使用 google-api-client 库,简单示例:

You can access the data using the google-api-client lib, simple example:

from apiclient.discovery import build
from apiclient.errors import HttpError
from oauth2client.tools import argparser

# Set DEVELOPER_KEY to the API key value from the APIs & auth > Registered apps
# tab of
#   https://cloud.google.com/console
# Please ensure that you have enabled the YouTube Data API for your project.
DEVELOPER_KEY = key
YOUTUBE_API_SERVICE_NAME = "youtube"
YOUTUBE_API_VERSION = "v3"


def youtube_search(vid):
    youtube = build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION,
                    developerKey=DEVELOPER_KEY)

    # Call the search.list method to retrieve results matching the specified
    # query term.
    search_response = youtube.search().list(
    part="snippet",
    relatedToVideoId=vid,
    type="video",
).execute()
print(search_response)
    print(search_response)


try:
    youtube_search("KQEOBZLx-Z8")
except HttpError as  e:
    print "An HTTP error %d occurred:\n%s" % (e.resp.status, e.content)

此处和下方列出了有效参数另一个完整的例子.您需要启用 youtube 数据 API 并拥有 开发人员密钥.

The valid parameters are listed here and below that is another full example. You will need to have the youtube data api enabled and to have a developer key.

使用请求:

import requests

url = "https://www.googleapis.com/youtube/v3/videos?part=snippet&id={id}&key={api_key}"
_id = "KQEOBZLx-Z8"
r = requests.get(url.format(id=_id, api_key=DEVELOPER_KEY))

print(r.json())

这给了你:

{u'etag': u'"5g01s4-wS2b4VpScndqCYc5Y-8k/8KOFImC75qsfDbVWk53UnuxsQmo"',
 u'items': [{u'etag': u'"5g01s4-wS2b4VpScndqCYc5Y-8k/60ikaVqe2cge-D1XrdM2l0Bf4Jg"',
             u'id': u'KQEOBZLx-Z8',
             u'kind': u'youtube#video',
             u'snippet': {u'categoryId': u'1',
                          u'channelId': u'UC3djj8jS0370cu_ghKs_Ong',
                          u'channelTitle': u'HooplaKidz',
                          u'description': u'Check out http://vid.io/xovq for Non-Stop Children Songs!\nEveryone likes to enjoy festivals, so come and sing along to this beautiful 12 Days of Christmas which will be loved by one and all !!! \n\nLyrics to Sing Along :-\n-------------------------------------\nOn the first day of Christmas, \nmy true love sent to me \nA partridge in a pear tree.\n \nOn the second day of Christmas, \nmy true love sent to me \nTwo turtle doves, \nAnd a partridge in a pear tree. \n\nOn the third day of Christmas, \nmy true love sent to me \nThree French hens, \nTwo turtle doves, \nAnd a partridge in a pear tree. \n\nOn the fourth day of Christmas, \nmy true love sent to me \nFour calling birds, \nThree French hens, \nTwo turtle doves, \nAnd a partridge in a pear tree. \n\nOn the fifth day of Christmas, \nmy true love sent to me \nFive golden rings, \nFour calling birds, \nThree French hens, \nTwo turtle doves, \nAnd a partridge in a pear tree. \n\nOn the sixth day of Christmas, \nmy true love sent to me \nSix geese a-laying, \nFive golden rings, \nFour calling birds, \nThree French hens, \nTwo turtle doves, \nAnd a partridge in a pear tree. \n\nOn the seventh day of Christmas, \nmy true love sent to me \nSeven swans a-swimming, \nSix geese a-laying, \nFive golden rings, \nFour calling birds, \nThree French hens, \nTwo turtle doves, \nAnd a partridge in a pear tree. \n\nOn the eighth day of Christmas, \nmy true love sent to me \nEight maids a-milking, \nSeven swans a-swimming, \nSix geese a-laying, \nFive golden rings, \nFour calling birds, \nThree French hens, \nTwo turtle doves, \nAnd a partridge in a pear tree. \n\nOn the ninth day of Christmas, \nmy true love sent to me \nNine ladies dancing, \nEight maids a-milking, \nSeven swans a-swimming, \nSix geese a-laying, \nFive golden rings, \nFour calling birds, \nThree French hens, \nTwo turtle doves, \nAnd a partridge in a pear tree. \n\nOn the tenth day of Christmas, \nmy true love sent to me \nTen lords a-leaping, \nNine ladies dancing, \nEight maids a-milking, \nSeven swans a-swimming, \nSix geese a-laying, \nFive golden rings, \nFour calling birds, \nThree French hens, \nTwo turtle doves, \nAnd a partridge in a pear tree. \n\nOn the eleventh day of Christmas, \nmy true love sent to me \nEleven pipers piping, \nTen lords a-leaping, \nNine ladies dancing, \nEight maids a-milking, \nSeven swans a-swimming, \nSix geese a-laying, \nFive golden rings, \nFour calling birds, \nThree French hens, \nTwo turtle doves, \nAnd a partridge in a pear tree. \n\nOn the twelfth day of Christmas, \nmy true love sent to me \nTwelve drummers drumming, \nEleven pipers piping, \nTen lords a-leaping, \nNine ladies dancing, \nEight maids a-milking, \nSeven swans a-swimming, \nSix geese a-laying, \nFive golden rings, \nFour calling birds, \nThree French hens, \nTwo turtle doves, \nAnd a partridge in a pear tree!  \n\nDownload Hooplakidz Songs on iTunes:\nhttp://itunes.apple.com/us/album/hoopla-kidz/id439987868\n\nBecome a Hooplakidz Fan on Facebook:\nhttp://www.facebook.com/hooplakidz',
                          u'liveBroadcastContent': u'none',
                          u'localized': {u'description': u'Check out http://vid.io/xovq for Non-Stop Children Songs!\nEveryone likes to enjoy festivals, so come and sing along to this beautiful 12 Days of Christmas which will be loved by one and all !!! \n\nLyrics to Sing Along :-\n-------------------------------------\nOn the first day of Christmas, \nmy true love sent to me \nA partridge in a pear tree.\n \nOn the second day of Christmas, \nmy true love sent to me \nTwo turtle doves, \nAnd a partridge in a pear tree. \n\nOn the third day of Christmas, \nmy true love sent to me \nThree French hens, \nTwo turtle doves, \nAnd a partridge in a pear tree. \n\nOn the fourth day of Christmas, \nmy true love sent to me \nFour calling birds, \nThree French hens, \nTwo turtle doves, \nAnd a partridge in a pear tree. \n\nOn the fifth day of Christmas, \nmy true love sent to me \nFive golden rings, \nFour calling birds, \nThree French hens, \nTwo turtle doves, \nAnd a partridge in a pear tree. \n\nOn the sixth day of Christmas, \nmy true love sent to me \nSix geese a-laying, \nFive golden rings, \nFour calling birds, \nThree French hens, \nTwo turtle doves, \nAnd a partridge in a pear tree. \n\nOn the seventh day of Christmas, \nmy true love sent to me \nSeven swans a-swimming, \nSix geese a-laying, \nFive golden rings, \nFour calling birds, \nThree French hens, \nTwo turtle doves, \nAnd a partridge in a pear tree. \n\nOn the eighth day of Christmas, \nmy true love sent to me \nEight maids a-milking, \nSeven swans a-swimming, \nSix geese a-laying, \nFive golden rings, \nFour calling birds, \nThree French hens, \nTwo turtle doves, \nAnd a partridge in a pear tree. \n\nOn the ninth day of Christmas, \nmy true love sent to me \nNine ladies dancing, \nEight maids a-milking, \nSeven swans a-swimming, \nSix geese a-laying, \nFive golden rings, \nFour calling birds, \nThree French hens, \nTwo turtle doves, \nAnd a partridge in a pear tree. \n\nOn the tenth day of Christmas, \nmy true love sent to me \nTen lords a-leaping, \nNine ladies dancing, \nEight maids a-milking, \nSeven swans a-swimming, \nSix geese a-laying, \nFive golden rings, \nFour calling birds, \nThree French hens, \nTwo turtle doves, \nAnd a partridge in a pear tree. \n\nOn the eleventh day of Christmas, \nmy true love sent to me \nEleven pipers piping, \nTen lords a-leaping, \nNine ladies dancing, \nEight maids a-milking, \nSeven swans a-swimming, \nSix geese a-laying, \nFive golden rings, \nFour calling birds, \nThree French hens, \nTwo turtle doves, \nAnd a partridge in a pear tree. \n\nOn the twelfth day of Christmas, \nmy true love sent to me \nTwelve drummers drumming, \nEleven pipers piping, \nTen lords a-leaping, \nNine ladies dancing, \nEight maids a-milking, \nSeven swans a-swimming, \nSix geese a-laying, \nFive golden rings, \nFour calling birds, \nThree French hens, \nTwo turtle doves, \nAnd a partridge in a pear tree!  \n\nDownload Hooplakidz Songs on iTunes:\nhttp://itunes.apple.com/us/album/hoopla-kidz/id439987868\n\nBecome a Hooplakidz Fan on Facebook:\nhttp://www.facebook.com/hooplakidz',
                                         u'title': u'12 Days of Christmas | Christmas Carols by Hooplakidz'},
                          u'publishedAt': u'2011-12-15T08:59:29.000Z',
                          u'tags': [u'12 days of christmas',
                                    u'Twelve Days Of Christmas (Holiday Period)',
                                    u'christmas carols',
                                    u'Christmas Carol (Composition Type)',
                                    u'christmas carols songs',
                                    u'christmas carols for children',
                                    u'christmas carols for kids',
                                    u'christmas songs',
                                    u'christmas songs for children',
                                    u'Christmas Music (Musical Genre)',
                                    u'Christmas (Holiday)',
                                    u'hooplakidz'],
                          u'thumbnails': {u'default': {u'height': 90,
                                                       u'url': u'https://i.ytimg.com/vi/KQEOBZLx-Z8/default.jpg',
                                                       u'width': 120},
                                          u'high': {u'height': 360,
                                                    u'url': u'https://i.ytimg.com/vi/KQEOBZLx-Z8/hqdefault.jpg',
                                                    u'width': 480},
                                          u'maxres': {u'height': 720,
                                                      u'url': u'https://i.ytimg.com/vi/KQEOBZLx-Z8/maxresdefault.jpg',
                                                      u'width': 1280},
                                          u'medium': {u'height': 180,
                                                      u'url': u'https://i.ytimg.com/vi/KQEOBZLx-Z8/mqdefault.jpg',
                                                      u'width': 320},
                                          u'standard': {u'height': 480,
                                                        u'url': u'https://i.ytimg.com/vi/KQEOBZLx-Z8/sddefault.jpg',
                                                        u'width': 640}},
                          u'title': u'12 Days of Christmas | Christmas Carols by Hooplakidz'}}],
 u'kind': u'youtube#videoListResponse',
 u'pageInfo': {u'resultsPerPage': 1, u'totalResults': 1}}

获取标题和类别 ID:

To get the title and categoryId:

r = requests.get(url.format(id=_id, api_key=DEVELOPER_KEY))
js = r.json()
items = js["items"][0]
print (items["snippet"]["categoryId"])
print (items["snippet"]["title"])

输出:

1
12 Days of Christmas | Christmas Carols by Hooplakidz

这将允许您以编程方式完成所有操作,创建 id 到类别名称的映射:

This will allow you to do it all programmatically, creating a mapping of id to category name:

import requests

def get_data(key, region, *ids):
    url = "https://www.googleapis.com/youtube/v3/videos?part=snippet&id={ids}&key={api_key}"
    r = requests.get(url.format(ids=",".join(ids), api_key=key))
    js = r.json()
    items = js["items"]
    cat_js = requests.get("https://www.googleapis.com/youtube/v3/videoCategories?part=snippet&regionCode={}&key={}".format(region,
        key)).json()
    categories = {d['id']: d["snippet"]['title'] for d in cat_js["items"]}
    for item in items:
        yield item["snippet"]["title"], categories[item["snippet"]["categoryId"]]

如果我们通过传入他需要的参数来运行它,我们会得到:

If we run it by passing in he required args we get:

In [86]: for title, cat in get_data(DEVELOPER_KEY, "IE",  "KQEOBZLx-Z8", "ALTc-Cqz650"):
   ....:         print(title, cat)
   ....:     
(u'12 Days of Christmas | Christmas Carols by Hooplakidz', u'Film & Animation')
(u'Conor Mcgregor Favorite to win Rematch against Nate Diaz,Lesnar on Drug addiction,Ali Funeral', u'Sports')

这篇关于使用 youtube api v3 从 url 获取 youtube 视频标题和类别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆