YouTube API 获取metadata

1.获取评论用户名、评论信息、ID、二级评论及热评点击次数。
python main.py --c --max 10000 --videourl "https://www.youtube.com/watch?v=NUI-mw5BsIg" --key AIzaSyD0ocit48msLTeRzJLJlOQet37vIBn_yZY

参考文章:https://github.com/srcecde/python-youtube-api

2.获取描述性信息

import urllib.request
import json #for decoding a JSON response
API_KEY = 'AIzaSyD0ocit48msLTeRzJLJlOQet37vIBn_yZY'
ChannelIdentifier = 'UC7REULPzdBo6T4QJrxAcUFA'
videoMetadata = []
with open(r"0.txt",'r') as f:
  for line in f:
    videoMetadata.append(line.strip('\n')) 
for metadata in videoMetadata:
  SpecificVideoID = metadata
  SpecificVideoUrl = 'https://www.googleapis.com/youtube/v3/videos?part=snippet%2CcontentDetails%2Cstatistics&id='+SpecificVideoID+'&key='+API_KEY
  response = urllib.request.urlopen(SpecificVideoUrl) #makes the call to a specific YouTube
  videos = json.load(response) #decodes the response so we can work with it
  videoMetadata = [] #declaring our list
  for video in videos['items']:
    if video['kind'] == 'youtube#video':
        print(metadata,video['snippet']['publishedAt'],video['contentDetails']['duration'],video['statistics']['viewCount'],video['statistics']['likeCount'],video['statistics']['dislikeCount'],video['statistics']['favoriteCount'],video['statistics']['commentCount'])

从txt文档中读取videoid输出metadata.py

# This python 2.7.14 example shows how to retrieve with Youtube API v3 a list of uploaded Youtube videos in a channel and also
# shows additional statistics of each individual youtube video such as number of views, likes etc.
# Please notice that YOU HAVE TO change API_KEY and Youtube channelID
# Have a look at the referred examples to get to understand how the API works
#
# The code consists of two parts:
# - The first part queries the videos in a channel and stores it in a list
# - The second part queries in detail each individual video
#
# Credits to the Coding 101 team, the guy previously guiding me to a query and Google API explorer who/which got me on track.
#
# RESULTING EXAMPLE OUTPUT: The output of the will look a bit like this:
#
# https://www.youtube.com/watch?v=T3U2oz_Y8T0
# Upload date:        2018-01-13T09:43:27.000Z
# Number of views:    8
# Number of likes:    2
# Number of dislikes: 0
# Number of favorites:0
# Number of comments: 0
#
# https://www.youtube.com/watch?v=EFyC8nhusR8
# Upload date:        2018-01-06T14:24:34.000Z
# Number of views:    6
# Number of likes:    2
# Number of dislikes: 0
# Number of favorites:0
# Number of comments: 0
#
#
import urllib #importing to use its urlencode function
import urllib  #for making http requests
import urllib.request
import json #for decoding a JSON response
#
API_KEY = 'AIzaSyD0ocit48msLTeRzJLJlOQet37vIBn_yZY'                 # What? How? Learn here: https://www.youtube.com/watch?v=JbWnRhHfTDA
ChannelIdentifier = 'UC7REULPzdBo6T4QJrxAcUFA' # What? How? Learn here: https://www.youtube.com/watch?v=tf42K4pPWkM
#
# This first part will query the list of videos uploaded of a specific channel
# The identification is done through the ChannelIdentifier hwich you have defined as a variable
# The results from this first part will be stored in the list videoMetadata. This will be used in the second part of the code below.
#
# This code is based on the a very good example from Coding 101 which you can find here:https://www.youtube.com/watch?v=_M_wle0Iq9M
#
url = 'https://www.googleapis.com/youtube/v3/search?part=snippet&channelId='+ChannelIdentifier+'&maxResults=500&pageToken='+'CDIQAA'+'&type=video&key='+API_KEY
#tokens = [‘none','CDIQAA','CGQQAA','CJYBEAA','CMgBEAA','CPoBEAA','CKwCEAA','CN4CEAA','CJADEAA','CMIDEAA','CPQDEAA','CKYEEAA','CNgEEAA','CIoFEAA','CLwFEAA','CO4FEAA','CKAGEAA','CNIGEAA','CIQHEAA','CLYHEAA']
# url中 CDIQAA表示为第二页,因每页最大获取maxresults为50个,所以需要指定pagetoken,第一页可默认缺省,第二页为CDIQAA,第三页为CGQQAA,以此类推。
response = urllib.request.urlopen(url) #makes the call to YouTube
videos = json.load(response) #decodes the response so we can work with it
videoMetadata = [] #declaring our list
for video in videos['items']:
  if video['id']['kind'] == 'youtube#video':
      videoMetadata.append(video['id']['videoId']) #Appends each videoID and link to our list
#
# In this second part, a loop will run through the listvideoMetadata
# During each step the details a specific video are retrieved and displayed
# The structure of the API-return can be tested with the API explorer (which you can excecute without OAuth):
# https://developers.google.com/apis-explorer/#p/youtube/v3/youtube.videos.list?part=snippet%252CcontentDetails%252Cstatistics&id=Ks-_Mh1QhMc&_h=1&
#
for metadata in videoMetadata:

  SpecificVideoID = metadata
  SpecificVideoUrl = 'https://www.googleapis.com/youtube/v3/videos?part=snippet%2CcontentDetails%2Cstatistics&id='+SpecificVideoID+'&key='+API_KEY
  response = urllib.request.urlopen(SpecificVideoUrl) #makes the call to a specific YouTube
  videos = json.load(response) #decodes the response so we can work with it
  videoMetadata = [] #declaring our list
  for video in videos['items']:
    if video['kind'] == 'youtube#video':

        print(metadata,video['snippet']['publishedAt'],video['contentDetails']['duration'],video['statistics']['viewCount'],video['statistics']['likeCount'],video['statistics']['dislikeCount'],video['statistics']['favoriteCount'],video['statistics']['commentCount'])

从channelid获取videoid输出metadata.py

最初版代码.py

3.待研究文章:
https://github.com/ripulagrawal98/Analytic_Steps

参考文章:
https://developers.google.com/youtube/v3/docs/videos
https://developers.google.com/youtube/v3/docs/search/list#maxResults
https://developers.google.com/youtube/v3/docs/search/list#pageToken
https://stackoverflow.com/questions/48253741/youtube-api-v3-and-python-to-generate-list-of-views-likes-on-own-youtube-videos
https://stackoverflow.com/questions/15596753/how-do-i-get-video-durations-with-youtube-api-version-3?rq=1
https://stackoverflow.com/questions/36314874/related-videos-with-part-contentdetails-and-statistics-youtube-api-v3
https://stackoverflow.com/questions/56389215/how-to-retrieve-large-amounts-of-data-5000-videos-from-youtube-data-api-v3
https://stackoverflow.com/questions/47593438/youtube-api-v3-returning-400-error-when-using-pagetoken
https://codebug.vip/questions-2918401.htm
http://www.yuanmacha.com/17363996516.html
https://stackoverflow.com/questions/14173428/how-to-change-page-results-with-youtube-data-api-v3

本文链接:

https://ma.ge/archives/41.html
1 + 7 =
快来做第一个评论的人吧~