How to output the number of VIEWs, likes, and stocks of articles posted on Qiita to CSV (created with "Python + Qiita API v2")

Preface

I will show you how to use ** Python ** and ** Qiita API v2 ** to output the number of VIEWs, likes, and stocks of your articles posted to Qiita to CSV.

Execution environment

Created program

Python program

It is a program that uses Qiita API v2 from Python to get the title, the number of VIEWs, the number of likes, and the number of stocks, and displays them in CSV format on the standard output.

Replace the xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx part of the program source below with your own access token. The access token can be obtained from "Settings"-"Applications"-"Personal Access Token" on the Qiita page.

getview.py


# -*- coding: utf-8 -*-
"""
Created on Wed Dec 11 23:44:00 2019

@author: yasushi-jp
"""

import requests
import json

url = 'https://qiita.com/api/v2/authenticated_user/items'
params = { "page" : "1", "per_page" : "100"}
headers = {"content-type" : "application/json", "Authorization" : "Bearer xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"}

res = requests.get(url, headers=headers, params=params)
list = res.json()

total_views_cnt = 0
total_likes_cnt = 0
total_stocks_cnt = 0

print("title,View count,Like count,Stock count")
for item in list:
    item_id = item['id']
    title = item['title']
    likes_cnt = item['likes_count']

    total_likes_cnt += likes_cnt

    url = 'https://qiita.com/api/v2/items/' + item_id
    res = requests.get(url, headers=headers)
    json = res.json()
    views_cnt = json['page_views_count']
    total_views_cnt += views_cnt

    url = 'https://qiita.com/api/v2/items/' + item_id + '/stockers'
    res = requests.get(url, headers=headers)
    users = res.json()
    stocks_cnt = len(users)
    total_stocks_cnt += stocks_cnt

    print(title + ", " + str(views_cnt) + ", " + str(likes_cnt) + ", " + str(stocks_cnt))
    
print("total, " + str(total_views_cnt) + ", " + str(total_likes_cnt) + ", " + str(total_stocks_cnt))

Bat for startup

I'm passing the path through the installed Python and calling the getview.py I just created to redirect the results to a file. Replace "C: \ DK \ Anaconda3 \ envs \ tf_env" with the directory where you installed Python.

getview.bat


@echo off
set PATH=%PATH%;C:\DK\Anaconda3\envs\tf_env
python getview.py > viewcount.csv
pause

Run

When you execute (double-click) getview.bat created above, viewcount.csv will be created.

reference

Qiita API v2 documentation

Recommended Posts

How to output the number of VIEWs, likes, and stocks of articles posted on Qiita to CSV (created with "Python + Qiita API v2")
Get the number of articles accessed and likes with Qiita API + Python
Get the number of PVs of Qiita articles you posted with API
Get a list of articles posted by users with Python 3 Qiita API v2
[Python] Automatically totals the total number of articles posted by Qiita using the API
[Python] Get the number of views of all posted articles
How to know the number of GPUs from python ~ Notes on using multiprocessing with pytorch ~
I tried to get the authentication code of Qiita API with Python.
How to count the number of elements in Django and output to a template
[First API] Try to get Qiita articles with Python
How to get the number of digits in Python
Try to estimate the number of likes on Twitter
How to output CSV of multi-line header with pandas
An easy way to pad the number with zeros depending on the number of digits [Python]
How to count the number of occurrences of each element in the list in Python with weight
After hitting the Qiita API with Python to get a list of articles for beginners, we will visit the god articles
Call the API of Hatena Blog from Python and save your blog articles individually on your PC
I tried to get the number of days of the month holidays (Saturdays, Sundays, and holidays) with python
How to scrape stock prices of individual stocks from the Nikkei newspaper website with Python
[Qiita API] [Statistics • Machine learning] I tried to summarize and analyze the articles posted so far.
How to display the CPU usage, pod name, and IP address of a pod created with Kubernetes
How to import CSV and TSV files into SQLite with Python
How to get followers and followers from python using the Mastodon API
[Python] How to get the first and last days of the month
I want to output the beginning of the next month with Python
Output the contents of ~ .xlsx in the folder to HTML with Python
To improve the reusability and maintainability of workflows created with Luigi
How to update the python version of Cloud Shell on GCP
How to send a request to the DMM (FANZA) API with python
How to run the practice code of the book "Creating a profitable AI with Python" on Google Colaboratory
[Python] How to specify the window display position and size of matplotlib
Put Cabocha 0.68 on Windows and try to analyze the dependency with Python
How to crop the lower right part of the image with Python OpenCV
How to get the date and time difference in seconds with python
[Introduction to Python] How to sort the contents of a list efficiently with list sort
How to put a line number at the beginning of a CSV file
[Python] How to use the enumerate function (extract the index number and element)
I tried to get the movie information of TMDb API with Python
(Diary 1) How to create, reference, and register data in the SQL database of Microsoft Azure service with python
GAE --With Python, rotate the image based on the rotation information of EXIF and upload it to Cloud Storage.
[Python] From morphological analysis of CSV data to CSV output and graph display [GiNZA]
How to get started with the 2020 Python project (windows wsl and mac standardization)
Summary of how to read numerical data with python [CSV, NetCDF, Fortran binary]
[Python] How to name table data and output it in csv (to_csv method)
[Python] How to save images on the Web at once with Beautiful Soup
How to use Service Account OAuth and API with Google API Client for python
Visualize the timeline of the number of issues on GitHub assigned to you in Python
Checklist on how to avoid turning the elements of numpy's array with for
Note: How to get the last day of the month with python (added the first day of the month)
How to get a list of files in the same directory with python
[Introduction to Python] How to get the index of data with a for statement
How to read a CSV file with Python 2/3
Output the number of CPU cores in Python
Scraping tabelog with python and outputting to CSV
Created a Python wrapper for the Qiita API
Strategy on how to monetize with Python Java
Read JSON with Python and output as CSV
Calculate the total number of combinations with python
How to specify attributes with Mock of python
How to erase the characters output by Python
Introduction to Python with Atom (on the way)