我正在使用python,我想使用YouTube API使用我的YouTube开发人员密钥和gdata来获取视频声明列表和查看计数。这可能吗?
答案 0 :(得分:0)
我想我的未经编辑的代码适用于您的应用程序。
<强>代码:强>
import requests
import json
import pandas
key = "xxx"
url = "https://www.googleapis.com/youtube/v3/search?part=snippet&order=viewCount&publishedAfter=2015-06-08T00%3A00%3A00Z&key="+key+\
"&type=video&maxResults=50&relevanceLanguage=en&videoDuration=short&videoCategoryId=15,18,23,44"
content = json.loads(requests.get(url).text)
def get_youtoube_content(content):
id = []
kind=[]
description = []
title = []
time =[]
for i in content["items"]:
a=i["id"]
b=i["snippet"]
id.append(a["videoId"])
kind.append(a["kind"])
description.append(b["description"])
title.append(b["title"])
time.append(b["publishedAt"])
data = pandas.DataFrame({"id":id, "type":kind,"description":description ,"title":title,"time":time })
return(data)
data=get_youtoube_content(content)
stat_url="https://www.googleapis.com/youtube/v3/videos?part=statistics&key="+key+"&maxResults=50&id="+str(list(data["id"])).replace("[","").replace("]","").replace("'","")
stat_content = json.loads(requests.get(stat_url).text)
def get_youtoube_content_stat(content):
id = []
commentCount=[]
dislikeCount = []
favoriteCount = []
likeCount=[]
viewCount=[]
for i in content["items"]:
id.append(i["id"])
b=i["statistics"]
commentCount.append(b["commentCount"])
dislikeCount.append(b["dislikeCount"])
favoriteCount.append(b["favoriteCount"])
likeCount.append(b["likeCount"])
viewCount.append(b["viewCount"])
data = pandas.DataFrame({"id":id, "commentCount":commentCount,"dislikeCount":dislikeCount ,"favoriteCount":favoriteCount,"likeCount":likeCount, "viewCount":viewCount })
return(data)
data_stat=get_youtoube_content_stat(stat_content)
data=pandas.merge(data, data_stat,"inner", on="id" )