Get the number of PVs of Qiita articles you posted with API

TL;DR

About the number of PVs of posted entries

If you look at the article you posted, the number of PV will be displayed.

image.png

It seems that you can only see the articles you posted.

That being said, I wondered if I could somehow get this all at once.

Qiita API v2

It seems that you can use the Qiita API to achieve this.

Qiita API v2 documentation

If you use the API related to "post", it seems that you can get it as page_views_count (views).

Post

When I looked it up, there were some articles that did the same thing, but I would like to write it myself for studying.

The theme is Python.

The following two Qiita APIs are used.

Obtaining an access token

First, get an access token.

Please issue an access token from "Settings" → "Applications".

image.png

The scope should have read_qiita this time.

image.png

Remember the value of the token you got here.

image.png

You can check the created access token as a list.

image.png

environment

This environment is here.

$ python3 -V
Python 3.6.8

For HTTP requests, we will use Requests.

$ pip3 install requests

Here is the version of Requests I used.

$ pip3 freeze
...
requests==2.22.0
...

Source code

Now, let's create the source code to get the number of PV.

I made it like this.

get_qiita_item_pv.py

from time import sleep
import requests

access_token = "[Created access token]"

item_list_base_url = "https://qiita.com/api/v2/authenticated_user/items"
headers = { "Authorization": f"Bearer {access_token}" }

page = 1
per_page = 100

sleep_time = 0.1

page_views = []

while True:
    item_list_url = f"{item_list_base_url}?page={page}&per_page={per_page}"

    items = requests.get(item_list_url, headers = headers)
    items_body = items.json()

    if not len(items_body):
        break

    for entry in items_body:
        item_id = entry["id"]

        item_url = f"https://qiita.com/api/v2/items/{item_id}"

        item = requests.get(item_url, headers = headers)
        item_body = item.json()

        title = item_body["title"]
        url = item_body["url"]
        tags = item_body["tags"]
        created_at = item_body["created_at"]
        page_views_count = item_body["page_views_count"]

        page_views.append({
            "title": title,
            "url": url,
            "tags": [ tag["name"] for tag in tags ],
            "created_at": created_at,
            "page_views_count": page_views_count
        })

        sleep(sleep_time)

    page += 1

    sleep(sleep_time)

page_views.sort(key = lambda e: e["page_views_count"], reverse = True)

for page_view in page_views:
    print(f"""page_view: {page_view['page_views_count']}
  title: {page_view['title']}
  url: {page_view['url']}
  tags: {page_view['tags']}
  created_at: {page_view['created_at']}""")
    print()

The number of PV cannot be obtained by the API of the article list (although there is an atmosphere that seems to be obtained by looking at the API document ...), it is actually necessary to call the API of each article. At this time, you will need your credentials (in short, an access token).

Run

Now let's run this script on our account.

The result is like this.

$ python3 get_qiita_item_pv.py 
page_view: 6154
  title: Ubuntu Linux 18.04 LTS / 18.I want to use OpenJDK 11 with 10
  url: https://qiita.com/charon/items/af0f0de2ae9adbc03bfe
  tags: ['Java', 'Linux', 'Ubuntu']
  created_at: 2018-11-19T13:03:18+09:00

page_view: 3846
  title:I want to do docker build under a proxy environment
  url: https://qiita.com/charon/items/d8365d610343d64d598e
  tags: ['Docker']
  created_at: 2019-01-04T11:29:29+09:00

page_view: 1826
  title:REQ with ZeroMQ (Python) for the first time-REP pattern
  url: https://qiita.com/charon/items/bdbef40fca6fa89edb24
  tags: ['Python', 'ZeroMQ']
  created_at: 2019-04-09T16:32:42+09:00

page_view: 1735
  title:Tail Apache access log with Fluentd and throw it into Elasticsearch
  url: https://qiita.com/charon/items/86d12ac7ca2d7cf6c580
  tags: ['Apache', 'Fluentd', 'Elasticsearch', 'Kibana']
  created_at: 2018-12-12T09:09:26+09:00

page_view: 1613
  title:QA site using Stack Overflow clone, Scoold
  url: https://qiita.com/charon/items/cacc2cfac380d8ad46b9
  tags: ['Java', 'AdventCalendar']
  created_at: 2018-12-11T14:34:57+09:00

page_view: 1385
  title:Send the log of the container started by Docker Compose to Amazon CloudWatch Logs
  url: https://qiita.com/charon/items/7e2328e5abf7340d32b6
  tags: ['Docker', 'CloudWatch-Logs']
  created_at: 2019-05-07T12:49:04+09:00

~abridgement~

It might be interesting to see what you see in the articles you wrote from time to time. I think.

Recommended Posts

Get the number of PVs of Qiita articles you posted with API
Get the number of articles accessed and likes with Qiita API + Python
[Python] Get the number of views of all posted articles
Get the number of views of Qiita
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
Get stock articles of infrastructure engineer yuta with Qiita API
I tried to get the authentication code of Qiita API with Python.
How to output the number of VIEWs, likes, and stocks of articles posted on Qiita to CSV (created with "Python + Qiita API v2")
[First API] Try to get Qiita articles with Python
Get the number of digits
Get the number of Youtube subscribers
After hitting the Qiita API with Python to get a list of articles for beginners, we will visit the god articles
Get the minutes of the Diet via API
Get holidays with the Google Calendar API
Count the number of characters with echo
A story that visualizes the present of Qiita with Qiita API + Elasticsearch + Kibana
Get the number of searches with a regular expression. SeleniumBasic VBA Python
Get the number of visits to each page with ReportingAPI + Cloud Functions
I tried to get the movie information of TMDb API with Python
Judge the authenticity of posted articles by machine learning (Google Prediction API).
Until you use the Kaggle API with Colab
Calculate the total number of combinations with python
Use twitter API to get the number of tweets related to a certain keyword
How to get the number of digits in Python
[Python] Get the text of the law from the e-GOV Law API
Get the sum of each of multiple columns with awk
Get the absolute path of the script you are running
Predict the number of people infected with COVID-19 with Prophet
Get the size (number of elements) of UnionFind in Python
Try to get the contents of Word with Golang
Get comments and subscribers with the YouTube Data API
Manage the package version number of requirements.txt with pip-tools
Get the operation status of JR West with Python
[Python] Get user information and article information with Qiita API
I built an application with Lambda that notifies LINE of "likes" using the Qiita API
I tried to get the number of days of the month holidays (Saturdays, Sundays, and holidays) with python
Get the trading price of virtual currency and create a chart with API of Zaif exchange
Get the id of a GPU with low memory usage
Get UNIXTIME at the beginning of today with a command
Get the number of specific elements in a python list
Let's touch the API of Netatmo Weather Station with Python. #Python #Netatmo
[Homology] Count the number of holes in data with Python
Let's visualize the number of people infected with coronavirus with matplotlib
Get the number of occurrences for each element in the list
Get the host name of the host PC with Docker on Linux
Get the source of the page to load infinitely with python.
10. Counting the number of lines
Get information with zabbix api
I touched the Qiita API
Calculate the number of changes
Call the API with python3.
Qiita API Oauth with Django
Get ranking with Rakuten API
How to get rid of the "Tags must be an array of hashes." Error in the qiita api
[Qiita API] [Statistics • Machine learning] I tried to summarize and analyze the articles posted so far.
I tried scraping the ranking of Qiita Advent Calendar with Python
How to get the ID of Type2Tag NXP NTAG213 with nfcpy
If you give a list with the default argument of the function ...
Consider what you can do with Python from the Qiita article
The story of visualizing popular Qiita tags with Bar Chart Race