[Python] Get user information and article information with Qiita API

Purpose of this article

Somehow, I wanted to analyze Qiita's article data, so I touched the API. You don't need to authenticate this time because you only need to get the article information.

I tried two big things.

    1. [Get user information list](# 1 Get user information list)
  1. [Getting a list of articles for a specific user](# 2 Getting a list of articles for a specific user)

I will explain in order.

Preparation

Load the library.

import numpy as np
import pandas as pd
import requests
import json
from pandas.io.json import json_normalize

The `` `json_normalize``` at the bottom is a convenient one that formats the json format data returned by the API into the pandas data frame format.

1. 1. Get user information list

For the sample Qiita API v2 documentation,

GET /api/v2/users?page=1&per_page=It says 20 etc..


 In other words, you can get information by accessing the following URL.
https://qiita.com/api/v2/users?page=1&per_page=20

 Here, `` `per_page``` is the number of users to get at one time, and `` `` page``` is the number. For example, if you want 1000 user information, `` `per_page`` You must send at least 10 requests with = 100 (upper limit) ```.


 So, the code looks like this:

```python
n = 333 #Number of users you want to get

per_page = 100
df = pd.DataFrame()

for page in range(1, int(n/per_page)+2): #Get a lot
    base_url = "https://qiita.com/api/v2/users?page={0}&per_page={1}"
    url = base_url.format(page, per_page)

    response = requests.get(url)
    res = response.json()

    tmp_df = json_normalize(res)
    df = pd.concat([df, tmp_df])

df.reset_index(drop=True, inplace=True)

df = df.iloc[:n,:] #Delete as much as you get

The result is a data frame like this: image.png

2. Get a list of articles for a specific user

For the sample Qiita API v2 documentation,

GET /api/v2/items?page=1&per_page=20&query=qiita+user%It says 3 Ayaotti etc..


 In other words, you can get information by accessing the following URL.
https://qiita.com/api/v2/items?page=1&per_page=20&query=qiita+user%3Ayaotti

 Now, a new `` `query``` has appeared, which gives you the same search options as when searching in a browser, where` ``: `` `is ``` `in the URL. Note that it is encoded in the notation% 3A```.

 ![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/540956/1b6f986d-87c0-8552-0c53-fedf902b54cc.png)

 By using this, you can get the information of a specific user with the feeling of ``` query = user% 3A 〇〇〇```.
 So the code looks like this:

```python
n = 125 #Number of articles you want to get

user = "yaotti"
per_page = 100
df = pd.DataFrame()

for page in range(1, int(n/per_page)+2): #Get a lot
    base_url = "https://qiita.com/api/v2/items?page={0}&per_page={1}&query=user%3A{2}"
    url = base_url.format(page, per_page, user)

    response = requests.get(url)
    res = response.json()

    tmp_df = json_normalize(res)
    df = pd.concat([df, tmp_df])

df.reset_index(drop=True, inplace=True)

df = df.iloc[:n,:] #Delete as much as you get

The result is a data frame like this: image.png

that's all!

reference

Qiita API v2 documentation Convert dictionary list to DataFrame with pandas json_normalize

Recommended Posts

[Python] Get user information and article information with Qiita API
[Python] Get Python package information with PyPI API
Get Gmail subject and body with Python and Gmail API
Get information with zabbix api
Get the number of articles accessed and likes with Qiita API + Python
Get Alembic information with Python
[First API] Try to get Qiita articles with Python
Get reviews with python googlemap api
Quine Post with Qiita API (Python)
Get Qiita trends with Python scraping
Get weather information with Python & scraping
[Python x Zapier] Get alert information and notify with Slack
Collecting information from Twitter with Python (Twitter API)
Get property information by scraping with python
Crawling with Python and Twitter API 2-Implementation of user search function
Get coincheck virtual currency information with API ♪
I tried to get the authentication code of Qiita API with Python.
I tried to get the movie information of TMDb API with Python
[Python] Qiita article information is pushed into mongoDB
Get stock price data with Quandl API [Python]
Get git branch name and tag name with python
How to get article data using Qiita API
Get CPU information of Raspberry Pi with Python
Recent ranking creation using Qiita API with Python
Try using ChatWork API and Qiita API in Python
Python script to get note information with REAPER
[google-oauth] Get user information
[Python] Get Qiita trends
[Python] Get product information such as ASIN and JAN with Amazon PA-API ver5.0
Get a list of articles posted by users with Python 3 Qiita API v2
Get media timeline images and videos with Python + Tweepy
Get schedule from Garoon SOAP API with Python + Zeep
Get comments on youtube Live with [python] and [pytchat]!
Get your current location and user agent in Python
Get comments and subscribers with the YouTube Data API
Get mail from Gmail and label it with Python3
Get country code with python
Programming with Python and Tkinter
Encryption and decryption with Python
Use Trello API with python
Python and hardware-Using RS232C with Python-
Get Youtube data with python
Get thread ID with python
Play RocketChat with API / Python
Get started with Python! ~ ② Grammar ~
Call the API with python3.
python with pyenv and venv
Use subsonic API with python3
[python] Get quotient and remainder
Get stock price with Python
Get home directory with python
Get keyboard events with python
Qiita API Oauth with Django
Works with Python and R
Get ranking with Rakuten API
YOLP Get map information XML file with Yahoo! Static Map API
Easy to use Nifty Cloud API with botocore and python
Try hitting the Twitter API quickly and easily with Python
Compare HTTP GET / POST with cURL (command) and Python (programming)
Beginners get Qiita tag information and visualize and consider the TOP10.
I tried follow management with Twitter API and Python (easy)