Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to obtain data in a table from Wikipedia API?

I'm trying to get all the content from Wikipedia:Unusual_articles and I'm able to get the list of table content by calling this endpoint:

https://en.wikipedia.org/w/api.php?action=parse&format=json&prop=sections&page=Wikipedia:Unusual_articles

and the data I got back look something like this:

{
    title: "Wikipedia:Unusual articles",
    pageid: 154126,
    sections: [
        {
            toclevel: 1,
            level: "2",
            line: "Places and infrastructure",
            number: "1",
            index: "T-1",
            fromtitle: "Wikipedia:Unusual_articles/Places_and_infrastructure",
            byteoffset: null,
            anchor: "Places_and_infrastructure"
        },
        {
            toclevel: 2,
            level: "3",
            line: "Americas",
            number: "1.1",
            index: "T-2",
            fromtitle: "Wikipedia:Unusual_articles/Places_and_infrastructure",
            byteoffset: null,
            anchor: "Americas"
        },
...

But I'm not able to get the content of a particular section. For example under Americas is a list of the table with a link and a short description, but is there a way to obtain the link and short description from the API?

table

like image 854
John Lim Avatar asked Oct 24 '16 04:10

John Lim


People also ask

What can you do with Wikipedia API?

Wikipedia API MASTER RECORD This provides developers code-level access to the entire Wikipedia reference. The goal of this API is to provide direct, high-level access to the data contained in the MediaWiki databases. Client programs can use the API to login, get data, and post changes.

How do I get an API page on Wikipedia?

There are three main methods for retrieving page content via the API: Get the contents of a page using the Revisions API (as wikitext). Get the contents of a page using the Parse API (as HTML or wikitext). Get plain text or limited HTML extracts of a page using the API of the TextExtracts extension.


1 Answers

You can get the content of every page section by using MediaWiki API with action=parse in two steps. First you have to get all sections from the page with:

https://en.wikipedia.org/w/api.php?action=parse&prop=sections&page=Wikipedia:Unusual_articles

From the response you see that section Americas has index=T-2 (T means transcluded page) and it comes from fromtitle=Wikipedia:Unusual_articles/Places_and_infrastructure. Now we use these index and fromtitle to get the content of the section with:

https://en.wikipedia.org/w/api.php?action=parse&page=Wikipedia:Unusual_articles/Places_and_infrastructure&section=2&prop=...

where:

  • prop=wikitext - gives the original section wikitext that was parsed.
  • prop=text - gives the parsed section text of the wikitext.
like image 175
Termininja Avatar answered Sep 28 '22 02:09

Termininja