使用youtube api v3从网址获取youtube视频标题和类别

时间:2016-07-04 11:54:20

标签: python youtube

我是初学者!我想获取YouTube视频的标题,也可以从python中的Url获取它的类别。

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天的圣诞节 - 圣诞节卡罗尔'

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

 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)  

我该怎么办?

1 个答案:

答案 0 :(得分:1)

您可以使用google-api-client lib访问数据,简单示例:

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)

有效参数列在here及以下,这是另一个完整示例。您需要启用 youtube数据api 并拥有开发人员密钥

使用请求:

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}}

获取标题和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到类别名称的映射:

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"]]

如果我们通过传递他需要的args来运行它,我们得到:

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')