Only the contributor can check the number of views of Qiita posted articles for each article. So, ** To know the total number of views of your post, go to all articles and calculate with a calculator OK **!
that's all.
I would have calculated it exactly one year ago.
However, it's been a year since I became an engineer. Such a method like a primitive man is not allowed.
I knew that I could get it using the API, so I looked it up.
Many articles have appeared, I couldn't find any articles about total views.
Finally, like the link below I would like to use GAS to perform automatic aggregation on a regular basis to complete oleore analytics.
[Qiita API] Like! Automatic counting of views ← Amazing article
20190208 postscript If it is exactly 100 articles, it will come out without passing except I added.
import requests
import json
import math
from msvcrt import getch
USER_ID = 'Your Qiita ID'
PER_PAGE = 20
allViews = 0
headers = {"content-type": "application/json",
'Authorization': 'Bearer your access token'}
#Send a request and insert a json that includes the total number of posts
url = 'https://qiita.com/api/v2/users/' + USER_ID
res = requests.get(url, headers=headers)
json_qiita_info = res.json()
#Pull out the number of posts from json
items_count = json_qiita_info['items_count']
#Calculate the number of pages
page = math.ceil(items_count / PER_PAGE)
print('|Article title|Number of views|Like count|')
#Get all posted articles
for i in range(page):
#Send a request and insert a json containing the information of each article
url = 'https://qiita.com/api/v2/authenticated_user/items' + \
'?page=' + str(i + 1)
res = requests.get(url, headers=headers)
json_qiita_info = res.json()
for j in range(PER_PAGE):
try:
#Pull ID out of json
item_id = json_qiita_info[j]['id']
#Send a request and insert a json containing the number of views of the article with the specified ID
url = 'https://qiita.com/api/v2/items/' + str(item_id)
res = requests.get(url, headers=headers)
json_view = res.json()
#Pull out the number of views from json
page_view = json_view['page_views_count']
#Addition and substitution to make the total number of views
allViews += page_view
#Display in order of title, number of likes, number of views
print('| ' + json_qiita_info[j]['title'] + ' | ' +
str(json_qiita_info[j]['likes_count']) + ' |' +
str(page_view) + ' |')
except IndexError:
print('View total:' + str(allViews))
print('Output complete')
getch()
break
#If it is exactly 100 articles, it will end without passing except
print('View total:' + str(allViews))
print('Output complete')
getch()
The title, number of likes, and number of views of each article are displayed, and finally the total number of views is don! is.
When I was investigating, ** "I was able to get the article information only with a GET request without an access token!" ** Because there was information that ** I thought, "The number of views can be completed only by a GET request that does not use an access token." **.
Also, in the official document of Qiita API v2
(GET / api / v2 / items), the value that exists in the field
There was a " page_views_count "
, so I got it and finished. .. ..
I thought, but it didn't work.
None will be returned as shown in the image.
If you think about it carefully, the number of views of each article can only be confirmed by the person who wrote the article. It was natural to say that it was not possible to obtain it with just a GET request.
How to get the correct number of views ** Use the request body including the access token in the GET request ** I understood it with the recognition that it was. (* The recognition of this area is ambiguous, so please point out any mistakes.)
The following is quoted from the official document of Qiita API v2
Parameters To request API v2, use 5 types of HTTP methods: GET, POST, PUT, PATCH, and DELETE. Requests to many APIs can include parameters, but if you want to include parameters in a GET request, use a URI query, otherwise use a request body. There are two types of parameters, one that is passed arbitrarily, such as pagination, and the other that is essential, such as the text at the time of posting. The API documentation describes the parameters that can be sent for each API.
Until I used the API, I was in a state of "what is paging?" I spent some time.
As the word paging says ** Turn over the book and look up the information on that page ** It means that it is necessary to implement ** book flipping ** ... in the process.
#Send a request and insert a json containing the information of each article
url = 'https://qiita.com/api/v2/authenticated_user/items' + '?page=' + str(i + 1)
res = requests.get(url, headers=headers)
json_qiita_info = res.json()
The parts corresponding to paging are shown separately for easy understanding.
This is where '? Page ='+ str (i + 1)
.
It's just sending a request to the page with the specified index.
I've spent the most time on this.
You may not notice that it contains half-width blank spaces,
The \
is missing at the end of the string. .. .. It was awkward to get an error.
Furthermore, since the character string is out of the correction range of the auto indent function
It took me a long time to notice the mistake first. .. ..
Try using Qiita API from Python Get a list of posts using Qiita API v2 [Python] Get list of posts using Qiita API + Review of 2018
Recommended Posts