我需要一些关于youtube api的解释。
在2.0版本中,像这样的url被用来获取配置文件的信息,但是现在2.0版本已经不支持了
http://gdata.youtube.com/feeds/base/users/[USERNAME]/uploads?alt=json&v=2&orderby=published&max-results=50
目前我正在使用这种行为来抓取数据,但最后一个json请求是被禁止的。
//查询用户频道
https://www.googleapis.com/youtube/v3/channels?part=contentDetails&forUsername={USERNAME}&key={API_KEY}
//搜索频道
https://www.googleapis.com/youtube/v3/search?part=id%2Csnippet&maxResults=50&channelId={CHANNEL}&key={API_KEY}
//搜索视频https://www.googleapis.com/youtube/v3/videos?part=id%2Csnippet%2CcontentDetails%2CfileDetails%2CliveStreamingDetails%2Cplayer%2CprocessingDetails%2CrecordingDetails%2Cstatistics%2Cstatus%2Csuggestions%2CtopicDetails&id={VIDEO_LIST}&key={API_KEY}
所以是否有办法从一个url抓取相同的数据为YouTube api版本3.0,有参数如USERNAME和API_KEY ?
这是使用Api 3.0抓取Youtube用户的最后50个(最大允许的)视频的方法
//查询用户通道
https://www.googleapis.com/youtube/v3/channels?part=contentDetails&forUsername={USERNAME}&key={KEY}
//搜索上传频道的播放列表项
https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&maxResults=50&playlistId={PLAYLIST}&key={KEY}
//搜索上传频道播放列表项目的视频
https://www.googleapis.com/youtube/v3/videos?part=id,snippet,contentDetails,status&id=BBfPP0rTjxo,o04cSB5afGc&maxResults=50&key={KEY}
版本3的YouTube数据API可以在这里找到:
https://developers.google.com/youtube/v3/GET https://www.googleapis.com/youtube/v3/channels?part=contentDetails&forUsername={USERNAME}&key={YOUR_API_KEY}
使用以下代码:
#!/usr/bin/python
from apiclient.discovery import build
from apiclient.errors import HttpError
DEVELOPER_KEY = "your key"
YOUTUBE_API_SERVICE_NAME = "youtube"
YOUTUBE_API_VERSION = "v3"
def youtube_search(search_item):
youtube = build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION,
developerKey=DEVELOPER_KEY)
search_response = {}
search_results = []
search_response = youtube.search().list(
q=search_item,
type="video",
part="id,snippet",
maxResults=50
).execute()
search_results.append(search_response)
if search_response.has_key('pageInfo'):
if search_response['pageInfo'].has_key('totalResults'):
totalResults = search_response['pageInfo']['totalResults']
for i in range(len(range(50,totalResults,50))):
if search_response.has_key('nextPageToken'):
pT = search_response['nextPageToken']
search_response = {}
search_response = youtube.search().list(
q=search_item,
type="video",
part="id,snippet",
pageToken = pT,
maxResults=50,
).execute()
print "ok"
time.sleep(1)
search_results.append(search_response)
for i in search_results:
for j in i['items']:
statistics = search_video(j['id']['videoId'])
j['statistics']=statistics
return search_results
def search_video(id):
youtube = build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION,
developerKey=DEVELOPER_KEY)
data = youtube.videos().list(
id=id,
part="statistics"
).execute()
return data['items'][0]['statistics']
def main():
#call the youtube_search function by passing the usernameof the user in youtube
#eg
#data = youtube_search('Google')
if __name__ == '__main__':
main()
使用此代码从api 3.0上的用户获取youtube上传视频的json数据。您将获得每个用户最多500个视频。api密钥将可用https://console.developers.google.com/