import os
import pandas as pd
import requests
from dotenv import load_dotenv
load_dotenv()
api_key = os.environ["OPENALEX_API_KEY"]OpenAlex APIをPythonから呼び出して論文の引用文献を取得する
APIキーの準備 (初めて利用する場合)
OpenAlex APIを利用するには、APIキーが必要です。 APIキーは、OpenAlexのアカウントを作成してから、設定画面で取得できます。 以下のリンクからサインアップして、APIキーを入手してください。
ライブラリのインポートとAPIキーの設定
今回は、Pythonの標準ライブラリである os と、外部ライブラリの requests と pandas を使います。 APIキーは、環境変数 OPENALEX_API_KEY に設定されていると仮定して、os.environ から取得します。 事前に作業ディレクトリの .env ファイルなどで OPENALEX_API_KEY=your_api_key_here と設定しておきます。 .envファイルは、プロジェクト直下に作成し、APIキーを含む環境変数を定義するためのテキストファイルです。 なお、GitHub Actionsを使う場合は、.envは使わず、Repository secrets にOPENALEX_API_KEYを登録して、workflowで渡すのが基本です。
.env
OPENALEX_API_KEY=your_api_key_hereDOIを指定して論文の引用文献を取得する
今回は、以下の論文を例にして、DOIを指定して論文の引用文献を取得する方法を説明します。
Yan, Pu, Nianpeng He, Kailiang Yu, Lawren Sack, Lin Jiang, and Marcos Fernández-Martínez. “Plant Elemental Diversity Increases Ecosystem Productivity and Temporal Stability.” Ecological Monographs 96, no. 1 (2026): e70061. https://doi.org/10.1002/ecm.70061.
DOIを指定して論文のメタデータを取得するには、以下のように /works/doi:{doi} エンドポイントを呼び出します。
その後、リクエストを作成し、requests.get() を使ってAPIを呼び出します。
doi = "10.1002/ecm.70061"
url = f"https://api.openalex.org/works/doi:{doi}"
params = {
"api_key": api_key,
"select": "id,doi,display_name,publication_year,referenced_works",
}
# DOIから対象論文を取得
response = requests.get(
url,
params=params,
timeout=30,
)
response.raise_for_status() # 失敗した場合は例外を発生させる結果はJSON形式で返されるため、response.json() を使ってPythonの辞書型に変換します。 レスポンスの中には、論文のタイトルや出版年、引用文献のIDなどが含まれています。
work = response.json()
print(work["display_name"]) # 論文のタイトルを表示
print(len(work["referenced_works"])) # 引用文献の数を表示出力結果
Plant elemental diversity increases ecosystem productivity and temporal stability
74
referenced_worksフィールドには、引用文献のIDがリスト形式で格納されています。
print(work["referenced_works"][:5]) # 最初の5件を表示出力結果
['https://openalex.org/W1584343945', 'https://openalex.org/W1594032928', 'https://openalex.org/W1885882554', 'https://openalex.org/W1965202842', 'https://openalex.org/W2020948897']このIDを使って、引用文献のメタデータを取得することができます。
引用文献のIDを使って、引用文献のメタデータを取得する
入手した引用文献のIDを使って、引用文献のタイトル、出版年、被引用数、DOIなどのメタデータを取得するには、以下のようにクエリパラメータを設定して、/works エンドポイントを呼び出します。
これにより、複数の引用文献の情報を一度に取得することができます。
ref_ids = work["referenced_works"]
params = {
"api_key": api_key,
"filter": "openalex:" + "|".join(ref_ids),
"per_page": 100,
"select": "id,display_name,publication_year,cited_by_count,doi",
}
response = requests.get(
"https://api.openalex.org/works",
params=params,
timeout=30,
)
response.raise_for_status()
refs = response.json()["results"]
df_refs = pd.DataFrame(
[
{
"title": ref.get("display_name"),
"year": ref.get("publication_year"),
"citations": ref.get("cited_by_count"),
"doi": ref.get("doi"),
"openalex_id": ref.get("id"),
}
for ref in refs
]
)
print(df_refs.to_string())出力結果の抜粋
title year citations doi openalex_id
0 Multimodel Inference 2004 11568 https://doi.org/10.1177/0049124104268644 https://openalex.org/W2158196600
1 A Caution Regarding Rules of Thumb for Variance Inflation Factors 2007 10409 https://doi.org/10.1007/s11135-006-9018-6 https://openalex.org/W2140964565
2 Soil Sampling and Methods of Analysis 2007 6452 https://doi.org/10.1201/9781420005271 https://openalex.org/W2478908224
3 performance: An R Package for Assessment, Comparison and Testing of Statistical Models 2021 5235 https://doi.org/10.21105/joss.03139 https://openalex.org/W3153999239
(途中省略)
71 The scaling of elemental stoichiometry and growth rate over the course of bamboo ontogeny 2023 17 https://doi.org/10.1111/nph.19408 https://openalex.org/W4388921950
72 Plant Elemental Homeostasis Enhances Species Performance and Community Functioning in Wetlands: Looking Beyond Nitrogen and Phosphorus 2025 4 https://doi.org/10.1111/ele.70152 https://openalex.org/W4412488598
73 Optimal set of leaf and aboveground tree elements for predicting forest functioning 2025 1 https://doi.org/10.5194/bg-22-2115-2025 https://openalex.org/W4409963066
このように、引用文献のIDを使って、引用文献のタイトル、出版年、被引用数、DOIなどのメタデータを取得することができます。