Get a list of articles posted by users with Python 3 Qiita API v2

Text

This is a sample code to get a list of articles posted by users with Python 3 and Qiita API v2.

In this post, I call the API every time to download JSON, but in reality it is better to save it locally and refer to it because of its kindness to the server (and the number of times limit).

import json
import urllib.request
from pprint import pp


def get_qiita_user_url(username):
    if not isinstance(username, str):
        raise TypeError()
    return f"https://qiita.com/api/v2/users/{username}"


def fetch_qiita_user(username):
    url = get_qiita_user_url(username)
    with urllib.request.urlopen(url) as f:
        return json.load(f)


def get_qiita_useritems_url(username, page, per_page):
    if not isinstance(username, str):
        raise TypeError()
    if not isinstance(page, int) or not isinstance(per_page, int):
        raise TypeError()
    if not (1 <= page <= 100) or not (1 <= per_page <= 100):
        raise ValueError()
    return f"https://qiita.com/api/v2/users/{username}/items?page={page}&per_page={per_page}"


def fetch_qiita_useritems(username, page, per_page):
    url = get_qiita_useritems_url(username, page, per_page)
    with urllib.request.urlopen(url) as f:
        return json.load(f)


def fetch_qiita_userallitems(username):
    userinfo = fetch_qiita_user(username)
    itemcount = userinfo["items_count"]
    useritems = []
    for page in range(1, 100):
        fetched = (page - 1) * 100
        if fetched > itemcount:
            break
        useritems.extend(fetch_qiita_useritems(username, page, 100))
        page += 1
    return useritems


#Specify your user name for the time being
username = "katabamisan"
useritems = fetch_qiita_userallitems(username)
titles = [entry["title"] for entry in useritems]
pp(titles)

Caution

According to the Qiita API v2 documentation, the API returns a response containing an object that represents an error on failure. On the other hand, Python 3's ʻurllib.request` raises an exception because the status code at this time is an error. This post doesn't specifically handle the error object, assuming that the caller of the function handles the exception.

Recommended Posts

Get a list of articles posted by users with Python 3 Qiita API v2
Get the number of PVs of Qiita articles you posted with API
Get a list of IAM users with Boto3
Get a list of Qiita likes by scraping
After hitting the Qiita API with Python to get a list of articles for beginners, we will visit the god articles
Get the number of articles accessed and likes with Qiita API + Python
[First API] Try to get Qiita articles with Python
[Python] Automatically totals the total number of articles posted by Qiita using the API
Get a list of purchased DMM eBooks with Python + Selenium
Get stock articles of infrastructure engineer yuta with Qiita API
[python] Get a list of instance variables
[Python] Get a list of folders only
Get a list of files in a folder with python without a path
How to output the number of VIEWs, likes, and stocks of articles posted on Qiita to CSV (created with "Python + Qiita API v2")
Get a list of packages installed in your current environment with python
I tried to get the authentication code of Qiita API with Python.
How to get a list of files in the same directory with python
Python: Get a list of methods for an object
Group by consecutive elements of a list in Python
[Python] Get user information and article information with Qiita API
[Python] Get the number of views of all posted articles
Get the value of a specific key in a list from the dictionary type in the list with Python
[Python] How to make a list of character strings character by character
Get the number of specific elements in a python list
Since Python 1.5 of Discord, I can't get a list of members
How to get a list of built-in exceptions in python
Python> Get a list of files in multiple directories> Use glob | Sort by modification time
Get a list of CloudWatch Metrics and a correspondence table for Unit units with Python boto
Get reviews with python googlemap api
Quine Post with Qiita API (Python)
Get Qiita trends with Python scraping
Try to get a list of breaking news threads in Python.
I tried to create a list of prime numbers with python
Pass a list by reference from Python to C ++ with pybind11
Get property information by scraping with python
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
Display a list of alphabets in Python 3
[Introduction to Python] How to sort the contents of a list efficiently with list sort
Hit a method of a class instance with the Python Bottle Web API
Receive a list of the results of parallel processing in Python with starmap
[Python] Get Python package information with PyPI API
I tried to get the movie information of TMDb API with Python
Implemented Python wrapper for Qiita API v2
Judge the authenticity of posted articles by machine learning (Google Prediction API).
Get users belonging to your organization from Garoon REST API with Python + Requests
Python script to get a list of input examples for the AtCoder contest
Get the stock price of a Japanese company with Python and make a graph
[Introduction to Python] How to get the index of data with a for statement
[AtCoder] Solve A problem of ABC101 ~ 169 with Python
Stock number ranking by Qiita tag with python
[Python] Get the files in a folder with Python
Get a ticket for a theme park with python
[python] Create a list of various character types
Get the caller of a function in Python
Make a copy of the list in Python
Get stock price data with Quandl API [Python]
A memorandum of extraction by python bs4 request
Solve A ~ D of yuki coder 247 with python
[Python] Get rid of dating with regular expressions
Get CPU information of Raspberry Pi with Python