15
17

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.

Wikipediaの解説(サマリー)をAPIで取得する

Last updated at Posted at 2020-05-03

Wikipediaの記事の解説(サマリー)と、写真(サムネイル)をAPIで取得する方法です。
文豪の書いた名作についての解説と、文豪の写真が欲しかったので利用しました。

参考:

Chrome の Developer Tools を使ってAPIの動作を確認することにします。新しいタブを開いて、Ctrl + Shift + I を押して Console を開き、次の JavaScript コードを入力します。

var url = '';
fetch(url)
  .then(response => response.json())
  .then(data => console.log(JSON.stringify(data, null, 2)));

この時、次のようなエラーが表示される場合があります。

Access to fetch at 'https://ja.wikipedia.org/w/api.php?...' from origin 'chrome-search://local-ntp' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

この場合は、利用する URL に&origin=*を付与してください。ただし、プロダクトコードでは利用しないように注意してください。

前提:Wikipedia のタイトルの作り方

夏目漱石の 吾輩は猫である は、同じ名前の作品は存在しないため、作品名のまま「吾輩は猫である」がタイトルになります。

吉川英治三国志 は、同じ名前の作品が存在するため、作品名の後にアンダーバー (_) 一つと、英字のカッコ()を使った「三国志_(吉川英治)」がタイトルになります。

一方、芥川龍之介羅生門は「羅生門_(小説)」がタイトルです。ややこしいですね。

アンダーバーが付く点と、カッコが英字の点に気を付けてください。

解説(サマリー)を取得する

API: query/prop=extractsを使います。
URLは次のようになります。

取得結果は次の通りです。

{
  "batchcomplete": "",
  "query": {
    "pages": {
      "13241": {
        "pageid": 13241,
        "ns": 0,
        "title": "吾輩は猫である",
        "extract": "『吾輩は猫である』(わがはいはねこである)は、夏目漱石の長編小説であり、処女小説である。1905年(明治38年)1月、『ホトトギス』に発表され、好評を博したため、翌1906年(明治39年)8月まで継続した。\n「吾輩は猫である。名前はまだ無い。どこで生れたかとんと見当がつかぬ。」という書き出しで始まり、中学校の英語教師である珍野苦沙弥の家に飼われている猫である「吾輩」の視点から、珍野一家や、そこに集う彼の友人や門下の書生たち、「太平の逸民」(第二話、第三話)の人間模様が風刺的・戯作的に描かれている。\n着想は、E.T.A.ホフマンの長編小説『牡猫ムルの人生観』と考えられている。\nまた『吾輩は猫である』の構成は、『トリストラム・シャンディ』の影響とも考えられている。"
      }
    }
  }
}

pagesの下が、めちゃくちゃアクセスしずらい構造です...。
extractにJavaScriptでアクセスするには、次のようなコードが必要です。

// 返却された JSON が data という変数にはいっている。
for (let id in data.query.pages) {
  console.log(data.query.pages[id].extract);
}

写真を取得する

REST API の/pages/summaryを使います。

こちらで取得できる解説(サマリー)は、先ほどの prop=extract のものよりも短いです。

{
  "type": "standard",
  "title": "吾輩は猫である",
  "displaytitle": "吾輩は猫である",
  "namespace": {
    "id": 0,
    "text": ""
  },
  "wikibase_item": "Q35690",
  "titles": {
    "canonical": "吾輩は猫である",
    "normalized": "吾輩は猫である",
    "display": "吾輩は猫である"
  },
  "pageid": 13241,
  "thumbnail": {
    "source": "https://upload.wikimedia.org/wikipedia/commons/d/d9/Ich_der_Kater_2.jpg",
    "width": 171,
    "height": 250
  },
  "originalimage": {
    "source": "https://upload.wikimedia.org/wikipedia/commons/d/d9/Ich_der_Kater_2.jpg",
    "width": 171,
    "height": 250
  },
  "lang": "ja",
  "dir": "ltr",
  "revision": "77131801",
  "tid": "b8832da0-85de-11ea-9c0b-3deb70fa928a",
  "timestamp": "2020-04-19T05:16:45Z",
  "description": "夏目漱石の小説",
  "description_source": "central",
  "content_urls": {
    "desktop": {
      "page": "https://ja.wikipedia.org/wiki/%E5%90%BE%E8%BC%A9%E3%81%AF%E7%8C%AB%E3%81%A7%E3%81%82%E3%82%8B",
      "revisions": "https://ja.wikipedia.org/wiki/%E5%90%BE%E8%BC%A9%E3%81%AF%E7%8C%AB%E3%81%A7%E3%81%82%E3%82%8B?action=history",
      "edit": "https://ja.wikipedia.org/wiki/%E5%90%BE%E8%BC%A9%E3%81%AF%E7%8C%AB%E3%81%A7%E3%81%82%E3%82%8B?action=edit",
      "talk": "https://ja.wikipedia.org/wiki/%E3%83%8E%E3%83%BC%E3%83%88:%E5%90%BE%E8%BC%A9%E3%81%AF%E7%8C%AB%E3%81%A7%E3%81%82%E3%82%8B"
    },
    "mobile": {
      "page": "https://ja.m.wikipedia.org/wiki/%E5%90%BE%E8%BC%A9%E3%81%AF%E7%8C%AB%E3%81%A7%E3%81%82%E3%82%8B",
      "revisions": "https://ja.m.wikipedia.org/wiki/Special:History/%E5%90%BE%E8%BC%A9%E3%81%AF%E7%8C%AB%E3%81%A7%E3%81%82%E3%82%8B",
      "edit": "https://ja.m.wikipedia.org/wiki/%E5%90%BE%E8%BC%A9%E3%81%AF%E7%8C%AB%E3%81%A7%E3%81%82%E3%82%8B?action=edit",
      "talk": "https://ja.m.wikipedia.org/wiki/%E3%83%8E%E3%83%BC%E3%83%88:%E5%90%BE%E8%BC%A9%E3%81%AF%E7%8C%AB%E3%81%A7%E3%81%82%E3%82%8B"
    }
  },
  "api_urls": {
    "summary": "https://ja.wikipedia.org/api/rest_v1/page/summary/%E5%90%BE%E8%BC%A9%E3%81%AF%E7%8C%AB%E3%81%A7%E3%81%82%E3%82%8B",
    "metadata": "https://ja.wikipedia.org/api/rest_v1/page/metadata/%E5%90%BE%E8%BC%A9%E3%81%AF%E7%8C%AB%E3%81%A7%E3%81%82%E3%82%8B",
    "references": "https://ja.wikipedia.org/api/rest_v1/page/references/%E5%90%BE%E8%BC%A9%E3%81%AF%E7%8C%AB%E3%81%A7%E3%81%82%E3%82%8B",
    "media": "https://ja.wikipedia.org/api/rest_v1/page/media/%E5%90%BE%E8%BC%A9%E3%81%AF%E7%8C%AB%E3%81%A7%E3%81%82%E3%82%8B",
    "edit_html": "https://ja.wikipedia.org/api/rest_v1/page/html/%E5%90%BE%E8%BC%A9%E3%81%AF%E7%8C%AB%E3%81%A7%E3%81%82%E3%82%8B",
    "talk_page_html": "https://ja.wikipedia.org/api/rest_v1/page/html/%E3%83%8E%E3%83%BC%E3%83%88:%E5%90%BE%E8%BC%A9%E3%81%AF%E7%8C%AB%E3%81%A7%E3%81%82%E3%82%8B"
  },
  "extract": "『吾輩は猫である』(わがはいはねこである)は、夏目漱石の長編小説であり、処女小説である。1905年(明治38年)1月、『ホトトギス』に発表され、好評を博したため、翌1906年(明治39年)8月まで継続した。",
  "extract_html": "<p>『<b>吾輩は猫である</b>』(わがはいはねこである)は、夏目漱石の長編小説であり、処女小説である。1905年(明治38年)1月、『ホトトギス』に発表され、好評を博したため、翌1906年(明治39年)8月まで継続した。</p>"
}

指定したタイトルの記事が見つからない場合

間違えて、タイトルに「吾輩は猫である_(夏目漱石)」を指定した場合を例にとって説明します。

prop=extract の場合は次のエラーが返ります。

{
  "batchcomplete": "",
  "query": {
    "normalized": [
      {
        "from": "吾輩は猫である_(夏目漱石)",
        "to": "吾輩は猫である (夏目漱石)"
      }
    ],
    "pages": {
      "-1": {
        "ns": 0,
        "title": "吾輩は猫である (夏目漱石)",
        "missing": ""
      }
    }
  }
}

RESET API の場合は次のエラーが返ります。

{
  "type": "https://mediawiki.org/wiki/HyperSwitch/errors/not_found",
  "title": "Not found.",
  "method": "get",
  "detail": "Page or revision not found.",
  "uri": "/ja.wikipedia.org/v1/page/summary/%E5%90%BE%E8%BC%A9%E3%81%AF%E7%8C%AB%E3%81%A7%E3%81%82%E3%82%8B_(%E5%A4%8F%E7%9B%AE%E6%BC%B1%E7%9F%B3)"
}
15
17
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
15
17

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?