10
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【Python】Youtubeの再生リストIDから動画情報を取得してExcelに書き出す

Last updated at Posted at 2021-03-11

これは何

タイトルのとおりです。せっかくなので置いときます(どこかに需要あるかもしれないし)
YoutubeAPI用のキーは各自用意してください。下記が参考になると思います
https://qiita.com/g-k/items/7c98efe21257afac70e9

概要

プレイリストIDから

  • 投稿日
  • タイトル
  • URL
  • 再生数
  • コメント数
  • 高評価数
  • 低評価数

をExcelのシートに書き出します。
一部のコードは下記を参考にしました。
https://qiita.com/kon2/items/f09def292c3b0be2bb8e

コード

そのまま貼り付けてAPIキーを入れると動きます
(足りないパッケージは適宜pip installしてください)

youtube_playlist.py
###############################################################################
# プレイリストから再生数、コメント数、高評価、低評価を入力する
# ファイルは時刻をくっつけて別名で保存される
###############################################################################

from googleapiclient.discovery import build
import json
import openpyxl
import re
import datetime
from datetime import date
from openpyxl.styles import Font

#================================================================================
# 初期化
#================================================================================
def initYoutube(API_KEY):
    API_SERVICE_NAME = "youtube"
    API_VERSION = "v3"
    return build(API_SERVICE_NAME, API_VERSION, developerKey=API_KEY)

#================================================================================
# プレイリストIDを渡して動画IDリストを得る
#================================================================================
def getIdListFromPlaylist(id_,youtube):

    nextPageToken = 'start'
    response = []

    while(nextPageToken is not None):

        if(nextPageToken == 'start'):
            search_response = youtube.playlistItems().list(
            part= 'snippet',
            playlistId=id_,
            maxResults = 50,
            ).execute()
        else:
            search_response = youtube.playlistItems().list(
            part= 'snippet',
            playlistId=id_,
            maxResults = 50,
            pageToken = nextPageToken
            ).execute()

        if('nextPageToken' in search_response):
            nextPageToken = search_response['nextPageToken']
        else:
            nextPageToken = None
        
        for item in search_response['items']:
            response.append(item['snippet']['resourceId']['videoId'])

 
    response.reverse()   
    return response

#================================================================================
# YoutubeのAPIを叩いて統計情報を取得する
#================================================================================
def getCountDetails(id_, youtube):

    #50件ずつに分割
    idLists = split_list(id_,50)
    response = []

    for idList in idLists:
        search_response = youtube.videos().list(
        part= 'statistics,snippet',
        id=idList,
        ).execute()

        response.extend(search_response['items'])

    return response

#================================================================================
# 指定ワークシートからIDリストを取得して数値を更新する
#================================================================================
def setCountDetail(ws,idList,youtube):

    result = getCountDetails(idList,youtube)

    row = 1
    ws.cell(row,1).value = '公開日'
    ws.cell(row,2).value = 'タイトル'
    ws.cell(row,3).value = 'URL'     
    ws.cell(row,4).value = '再生数'
    ws.cell(row,5).value = 'コメント数'
    ws.cell(row,6).value = '高評価'
    ws.cell(row,7).value = '低評価'

    row += 1

    for item in result:

        published = datetime.datetime.fromisoformat(item['snippet']['publishedAt'].replace('Z', '+00:00')).strftime('%Y/%m/%d')
        ws.cell(row,1).value = published
        ws.cell(row,2).value = item['snippet']['title']
        ws.cell(row,3).value = 'https://www.youtube.com/watch?v='+item['id']

        #コメント非公開等あるとそもそも値がないので、その場合は-1にしておく
        ws.cell(row,4).value = int(item['statistics']['viewCount']) if 'viewCount' in item['statistics'] else -1
        ws.cell(row,5).value = int(item['statistics']['commentCount'])  if 'commentCount' in item['statistics'] else -1
        ws.cell(row,6).value = int(item['statistics']['likeCount'])  if 'likeCount' in item['statistics'] else -1
        ws.cell(row,7).value = int(item['statistics']['dislikeCount']) if 'dislikeCount' in item['statistics'] else -1

        #ws.cell(row,3).hyperlink = ws.cell(row,3).value
        ws.cell(row,4).number_format = '#,##0'
        ws.cell(row,5).number_format = '#,##0'
        ws.cell(row,6).number_format = '#,##0'
        ws.cell(row,7).number_format = '#,##0'

        row += 1

#================================================================================
# 配列を指定した個数ごとに分割
#================================================================================
def split_list(l, n):
    for idx in range(0, len(l), n):
        yield l[idx:idx + n]



# YoutubeAPI用キー
API_KEY = '' #ここに各々で取得したYoutube用のAPIキーを入れる
FILENAME = 'Youtube'
youtube = initYoutube(API_KEY)

#「ワークシート名:プレイリストID」の辞書型配列にしておく
playList = {
'伊沢おすすめ動画':'PLWPrDZ_jwb2TTaZUz5CVl2fDcOYhcyn-R',
'笹木咲パワプロ':'PLed6q99qvCSdPtJYagHCISUbfSP-w2AI6'
}

# Excelファイル新規作成
wb = openpyxl.Workbook()

for key in playList:
    wb.create_sheet(key,0)
    setCountDetail(wb[key],getIdListFromPlaylist(playList[key],youtube),youtube)    

#タイムスタンプ付けて保存
dt_now = datetime.datetime.now()
wb.save(FILENAME+dt_now.strftime('_%Y%m%d_%H%M')+'.xlsx')
10
10
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?