5
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Python Qiita API を使ってキーワード検索

Last updated at Posted at 2021-02-10

Python Qiita API を使ってキーワード検索

Qiita API ドキュメント
https://qiita.com/api/v2/docs

アクセストークンの取得

設定 > アプリケーションからアクセストークンを発行
https://qiita.com/settings/applications

検索クエリ

page ... ページ番号(1~100)
per_page ... 1ページあたりに含まれる要素数
query ... 検索クエリ
検索オプション ... https://help.qiita.com/ja/articles/qiita-search-options

例:PHPタグを含み作成日付2021-02-09の投稿を取得
注)日付検索条件はUTC時刻を元に行われる模様

https://qiita.com/api/v2/items?page=1&per_page=100&query=tag:PHP+created:>=2021-02-09+created:<=2021-02-09

Python取得

QIITA_ACCESS_TOKENは アクセストークンの取得 で取得
リクエストして結果をJsonで取得
DataFrameに保存

search = qiita.get_search("Python", "2021-02-08", "2021-02-10")
QIITA_ACCESS_TOKEN = ""
PER_PAGE = 100


def get_search(tag: str, start_date: str, end_date: str, count: int = 1000):

    df = pd.DataFrame(
        columns=[
            "created_at",
            "id",
            "title",
            "likes_count",
            "url",
            "user_id",
            "user_name",
        ]
    )

    loop = ceil(count / PER_PAGE)
    page = 1

    for v in range(loop):

        page += v
        per_page = PER_PAGE if count - len(df) > PER_PAGE else count - len(df)

        url = (
            f"https://qiita.com/api/v2/items?page={page}&per_page={per_page}&"
            f"query=tag:{tag}+created:>={start_date}+created:<={end_date}"
        )
        headers = {"Authorization": f"Bearer {QIITA_ACCESS_TOKEN}"}
        response = requests.get(url, headers=headers)
        text = json.loads(response.text)

        if not text:
            break

        for v2 in text:
            df = df.append(
                {
                    "created_at": v2["created_at"],
                    "id": v2["id"],
                    "title": v2["title"],
                    "likes_count": v2["likes_count"],
                    "url": v2["url"],
                    "user_id": v2["user"]["id"],
                    "user_name": v2["user"]["name"],
                },
                ignore_index=True,
            )

    return df

結果

                   created_at                    id  ...              user_id       user_name
0   2021-02-10T22:39:34+09:00  f64d7918bb3e39e897a4  ...          tancematrix
1   2021-02-10T22:33:24+09:00  7c1ffc2112252730743c  ...           k_yokozuka  yokozuka kento
2   2021-02-10T22:32:07+09:00  6f2213e9ecfcfc37d5b6  ...           k_yokozuka  yokozuka kento
3   2021-02-10T22:02:35+09:00  28f12e509f10f35fd9cb  ...       RyosukeHattori          Raylan
4   2021-02-10T21:27:46+09:00  6a390e86ff8bd22aa5e8  ...             AKpirion
..                        ...                   ...  ...                  ...             ...
87  2021-02-08T11:26:30+09:00  dae3d01cb1ff6ecb074e  ...       ohisama@github
88  2021-02-08T11:02:58+09:00  aaeb0dccbb30e17bfba9  ...                etern
89  2021-02-08T10:58:13+09:00  2d14c71178f834dfc985  ...       ohisama@github
90  2021-02-08T10:13:23+09:00  1f90724a797f1b63a9c9  ...  morita-toyscreation  TAKASHI MORITA
91  2021-02-08T10:00:57+09:00  ff32350710617bd50544  ...               yudwig

Qiita投稿を取得できました
いいね!と思ったら LGTM お願いします :clap::clap::clap:

【PR】週末ハッカソンというイベントやってます! → https://weekend-hackathon.toyscreation.jp/about/

5
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?