Created
December 1, 2023 07:18
-
-
Save kkeeth/52205760419fc93d685de865242fc890 to your computer and use it in GitHub Desktop.
Qiita organization 記事一覧取得スクリプト
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import csv | |
from datetime import datetime | |
# Qiita APIの基本URLと組織の名前 | |
base_url = "https://qiita.com/api/v2" | |
organization_name = "yumemi" # ここに組織名を設定 | |
# 現在の年を取得 | |
current_year = datetime.now().year | |
# CSVファイルに保存するための準備 | |
csv_file = open("qiita_articles.csv", "w", newline="", encoding="utf-8") | |
csv_writer = csv.writer(csv_file) | |
csv_writer.writerow(["Title", "Date", "Likes", "URL", "Author"]) | |
# ページごとにリクエストを送る | |
for page in range(1, 4): # 3ページ分を取得(必要に応じて調整) | |
# 検索用のURLを作成 | |
search_url = f"{base_url}/items?page={page}&per_page=100&query=organization:{organization_name} created:>={current_year}-01-01" | |
response = requests.get(search_url) | |
# ステータスコードのチェック | |
if response.status_code == 200: | |
articles = response.json() | |
# 各記事の必要なデータをCSVに書き出す | |
for article in articles: | |
title = article["title"] | |
created_at = article["created_at"] | |
likes_count = article["likes_count"] | |
url = article["url"] | |
author = article["user"]["id"] # 投稿者のID | |
csv_writer.writerow([title, created_at, likes_count, url, author]) | |
else: | |
print(f"Error on page {page}: Unable to fetch data from Qiita API.") | |
break | |
# ファイルを閉じる | |
csv_file.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment