您可以使用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}}
获取title和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®ionCode={}&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"]]
如果我们通过传入 he required 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')