我正在尝试将来自youtube播放列表的视频存储到python中的列表中。我按照文档https://developers.google.com/youtube/v3/docs/playlistItems/list中的说明操作,但似乎无法检索视频ID。
def list_playlist_localizations(youtube, playlist_id):
results = youtube.playlists().list(
part='snippet, contentDetails', id=playlist_id, maxResults=5
).execute()
#print results
for playlist_item in results['items']:
title = playlist_item['snippet']['title']
video_id = playlist_item['snippet']['resourceId']['videoId']
print video_id
编辑:
from hyper import HTTPConnection
import random
import requests
playlist_id = "PLx0sYbCqOb8TBPRdmBHs5Iftvv9TPboYG"
links = []
token = '' # to get ID to next page CDIQAA
# https://stackoverflow.com/questions/14173428/how-to-change-page-results-with-youtube-data-api-v3 - reference
s = requests.Session()
r = s.get('https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&maxResults=50&playlistId=' + playlist_id + '&key={MY_KEY}')
data = r.json()
while 'nextPageToken' in data and len(data) < 200:
token = data['nextPageToken']
r = s.get('https://www.googleapis.com/youtube/v3/playlistItems?pageToken='
+ token +
'&part=snippet&maxResults=50&playlistId='
+ playlist_id +
'&key=AIzaSyAglPb2-eVaoy1muXclkUrfPFtdYEaUcCw')
data = r.json()
for items in data['items']:
links.append('https://www.youtube.com/watch?v=' + items['snippet']['resourceId']['videoId'])
r = s.get('https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&maxResults=50&playlistId=' + playlist_id + '&key={MY_KEY}')
data = r.json()
# Add the remaining links
for items in data['items']:
links.append('https://www.youtube.com/watch?v=' + items['snippet']['resourceId']['videoId'])
'''
for items in links:
print items
'''
print "num items: " + str(len(links))
print "Random link: "
print links[random.randint(0, len(links) - 1)]
这是我从播放列表中生成随机YouTube链接的实现
答案 0 :(得分:2)
您可以使用html请求获取数据
并且您将获得播放列表作为JSON数据
例如
import requests,json
key = 'YOUR API KEY'
playlistid = 'PLBCF2DAC6FFB574DE'
data = json.loads(requests.get('https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&playlistId={}&key={}'.format(playlistid,key)).text)
print(data)
重生将是
{
"kind": "youtube#playlistItemListResponse",
"etag": "\"Wu2llbfqCdxIVjGbVPm2DslKPCA/_HwI6Wa-Ku-4k5VAHT1GRPH2his\"",
"nextPageToken": "CAUQAA",
"pageInfo": {
"totalResults": 13,
"resultsPerPage": 5
},
"items": [
{
"kind": "youtube#playlistItem",
"etag": "\"Wu2llbfqCdxIVjGbVPm2DslKPCA/4MX9K88_OdrfvVVaQ2ys5BdQtI8\"",
"id": "UExCQ0YyREFDNkZGQjU3NERFLkE2OTA3QzIwNEI3RjYxMDE=",
"snippet": {
"publishedAt": "2013-10-18T14:55:24.000Z",
"channelId": "UCvceBgMIpKb4zK1ss-Sh90w",
"title": "Andrew Willis, Skatepark Engineer",
"description": "Andrew Willis built a skatepark in East London using reclaimed materials left over from the Olympic Games, creating a lasting legacy for the local community of Hackney Wick.",
"thumbnails": {
"default": {
"url": "https://i.ytimg.com/vi/GvgqDSnpRQM/default.jpg",
"width": 120,
"height": 90
},
"medium": {
"url": "https://i.ytimg.com/vi/GvgqDSnpRQM/mqdefault.jpg",
"width": 320,
"height": 180
},
"high": {
"url": "https://i.ytimg.com/vi/GvgqDSnpRQM/hqdefault.jpg",
"width": 480,
"height": 360
},
"standard": {
"url": "https://i.ytimg.com/vi/GvgqDSnpRQM/sddefault.jpg",
"width": 640,
"height": 480
}
},
"channelTitle": "Google Search Stories",
"playlistId": "PLBCF2DAC6FFB574DE",
"position": 0,
"resourceId": {
"kind": "youtube#video",
"videoId": "GvgqDSnpRQM"
}
}
},
{
"kind": "youtube#playlistItem",
"etag": "\"Wu2llbfqCdxIVjGbVPm2DslKPCA/PBY3vMCXQ8JLWA2_X9BSSpasHUo\"",
"id": "UExCQ0YyREFDNkZGQjU3NERFLjU5NzE2QkNERURDRTE5NDc=",
"snippet": {
"publishedAt": "2011-09-23T04:52:44.000Z",
"channelId": "UCvceBgMIpKb4zK1ss-Sh90w",
"title": "Private video",
"description": "This video is private.",
"channelTitle": "Google Search Stories",
"playlistId": "PLBCF2DAC6FFB574DE",
"position": 1,
"resourceId": {
"kind": "youtube#video",
"videoId": "fIc_VEQ7Vo0"
}
}
},
{
"kind": "youtube#playlistItem",
"etag": "\"Wu2llbfqCdxIVjGbVPm2DslKPCA/y4jkKn0lC3ggyxUi8Xr1NXcGPjA\"",
"id": "UExCQ0YyREFDNkZGQjU3NERFLkZGQjNGMEFDQ0U0MzBCMzc=",
"snippet": {
"publishedAt": "2011-11-22T15:29:40.000Z",
"channelId": "UCvceBgMIpKb4zK1ss-Sh90w",
"title": "Mark Lesek: A New/Old Prosthetic",
"description": "Follow Mark on Google+: https://plus.google.com/u/0/114778778979884307299/about\r\n\r\nMark lost his arm several years ago. Now his search for a better prosthetic could improve the lives of amputees everywhere.",
"thumbnails": {
"default": {
"url": "https://i.ytimg.com/vi/V4DDt30Aat4/default.jpg",
"width": 120,
"height": 90
},
"medium": {
"url": "https://i.ytimg.com/vi/V4DDt30Aat4/mqdefault.jpg",
"width": 320,
"height": 180
},
"high": {
"url": "https://i.ytimg.com/vi/V4DDt30Aat4/hqdefault.jpg",
"width": 480,
"height": 360
},
"standard": {
"url": "https://i.ytimg.com/vi/V4DDt30Aat4/sddefault.jpg",
"width": 640,
"height": 480
}
},
"channelTitle": "Google Search Stories",
"playlistId": "PLBCF2DAC6FFB574DE",
"position": 2,
"resourceId": {
"kind": "youtube#video",
"videoId": "V4DDt30Aat4"
}
}
},
{
"kind": "youtube#playlistItem",
"etag": "\"Wu2llbfqCdxIVjGbVPm2DslKPCA/9uUzwGG9HqDu7g3ZMZw3TYWlBOs\"",
"id": "UExCQ0YyREFDNkZGQjU3NERFLkFBRDU2ODRCNTgwMzA2RTQ=",
"snippet": {
"publishedAt": "2011-10-20T01:02:11.000Z",
"channelId": "UCvceBgMIpKb4zK1ss-Sh90w",
"title": "Private video",
"description": "This video is private.",
"channelTitle": "Google Search Stories",
"playlistId": "PLBCF2DAC6FFB574DE",
"position": 3,
"resourceId": {
"kind": "youtube#video",
"videoId": "07718Vcwcyc"
}
}
},
{
"kind": "youtube#playlistItem",
"etag": "\"Wu2llbfqCdxIVjGbVPm2DslKPCA/x8utLR6SK07clGh9DQzv80Mczyw\"",
"id": "UExCQ0YyREFDNkZGQjU3NERFLkMyQjUzQkM1OTFFRTNFMEQ=",
"snippet": {
"publishedAt": "2011-11-22T15:29:40.000Z",
"channelId": "UCvceBgMIpKb4zK1ss-Sh90w",
"title": "Mark Kempton: Neighbors In Need",
"description": "Follow Mark on Google+: https://profiles.google.com/u/0/105705606437451864842\r\n\r\nWhen floodwaters hit northeast Australia, Mark's innovative search became the difference between life and death for many of his neighbors.",
"thumbnails": {
"default": {
"url": "https://i.ytimg.com/vi/XDgC4FMftpg/default.jpg",
"width": 120,
"height": 90
},
"medium": {
"url": "https://i.ytimg.com/vi/XDgC4FMftpg/mqdefault.jpg",
"width": 320,
"height": 180
},
"high": {
"url": "https://i.ytimg.com/vi/XDgC4FMftpg/hqdefault.jpg",
"width": 480,
"height": 360
},
"standard": {
"url": "https://i.ytimg.com/vi/XDgC4FMftpg/sddefault.jpg",
"width": 640,
"height": 480
}
},
"channelTitle": "Google Search Stories",
"playlistId": "PLBCF2DAC6FFB574DE",
"position": 4,
"resourceId": {
"kind": "youtube#video",
"videoId": "XDgC4FMftpg"
}
}
}
]
}