2
2

More than 1 year has passed since last update.

PythonとYouTubeAPIで急上昇の動画のデータを取ってくる

Posted at

やること

YouTubeAPIで急上昇動画のURLを取ってくる。

まず、GoogleCloudPlatformに行き、プロジェクトを作成して、YouTube Data v3というAPIを有効化します。「認証情報」をクリックして「認証情報」を作成を選び、「APIキー」を選択します。出てきたキーはコピーして取っておきます。

import googleapiclient.discovery

def getYouTubeTop10():
    API_KEY = "あなたのAPIキー"
    youtube = googleapiclient.discovery.build(
        "youtube", "v3", developerKey=API_KEY)
    request = youtube.videos().list(
        part="snippet,contentDetails,statistics",
        chart="mostPopular",
        maxResults=10, # ここで個数を指定する
        regionCode="JP" # ここで場所(国)の指定
    )
    response = request.execute()
    return response

def writeData(json_file, data_dict):
    # 辞書型をjsonに変換して保存
    with open(json_file, "w", encoding="utf-8") as f:
        json.dump(data_dict, f, indent="\t", ensure_ascii=False)

writeData("youtubetop10.json", getYouTubeTop10())

youtubetop10.jsonに、データが保存されます。

2
2
0

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
2
2