Get Youtube data with python

Introduction

I often see the method of using google-api-python-client as a method to get Youtube data such as the number of views by specifying videoId in pyhton. Here, instead of that method, I tried to get it with https using requests.

It is assumed that the API key of Youtube Data API has been obtained.

URL format

Youtube data can be obtained in the following URL format. www.googleapis.com/youtube/v3/videos?part=statistics&id=★ID★&fields=items%2Fstatistics&key=★APIキー★

ID ... videoId API key ... API key for Youtube Data API

Return value

The result will be returned in Json format as follows. {'items': [{'statistics': {'viewCount': '267', 'likeCount': '3', 'dislikeCount': '0', 'favoriteCount': '0', 'commentCount': '0'}}]} If you want to get only the number of views (viewCount) You can get it below assuming that res contains the result. count = res["items"][0]["statistics"]["viewCount"]

sample

sample.py


#!/usr/bin/python
# -*- coding: utf-8 -*-
import requests
import json

base_url = "https://www.googleapis.com/youtube/v3/videos?part=statistics&id={}&fields=items%2Fstatistics&key={}"

api_key  = "xxxx"
id = "xxxxx"
url = base_url.format(id,api_key)
res = json.loads(requests.get(url,verify=False).text)
count = res["items"][0]["statistics"]["viewCount"]

print(count)

Confirmation environment

Windows10 Python 3.7.0

Recommended Posts

Get Youtube data with python
[Python] Get economic data with DataReader
Get additional data in LDAP with python
Data analysis with python 2
[Note] Get data from PostgreSQL with Python
Get Youtube data in Python using Youtube Data API
Get date with python
Data analysis with Python
Get stock price data with Quandl API [Python]
I tried to get CloudWatch data with Python
Get country code with python
Get Twitter timeline with python
YouTube video management with Python 3
Get thread ID with python
Get started with Python! ~ ② Grammar ~
Get stock price with Python
Get home directory with python
Get keyboard events with python
Get Alembic information with Python
Read json data with python
Get financial data with python (then a little tinkering)
Get comments on youtube Live with [python] and [pytchat]!
Get comments and subscribers with the YouTube Data API
Get data from database via ODBC with Python (Access)
Get Leap Motion data in Python.
Link to get started with python
Get data from Quandl in Python
Get the weather with Python requests
Get web screen capture with python
Get the weather with Python requests 2
How to get started with Python
Python data structures learned with chemoinformatics
[Small story] Get timestamp with Python
Get Qiita trends with Python scraping
Easy data visualization with Python seaborn.
Process Pubmed .xml data with python
Data analysis starting with python (data visualization 1)
Get started with Python in Blender
Data analysis starting with python (data visualization 2)
Get weather information with Python & scraping
Python application: Data cleansing # 2: Data cleansing with DataFrame
Get rid of dirty data with Python and regular expressions
Get data from analytics API with Google API Client for python
Get additional data to LDAP with python (Writer and Reader)
[Introduction to Python] How to get data with the listdir function
Play with YouTube Data API v3 using Google API Python Client
Get property information by scraping with python
Data pipeline construction with Python and Luigi
FizzBuzz with Python3
Receive textual data from mysql with python
Get html from element with Python selenium
Scraping with Python
Statistics with python
Process Pubmed .xml data with python [Part 2]
Scraping with Python
Add a Python data source with Redash
Retrieving food data with Amazon API (Python)
Python with Go
Try working with binary data in Python
Data analysis python
Generate Japanese test data with Python faker