How can I get the Infobox from a Wikipedia article by the MediaWiki API? [duplicate]
Asked Answered
C

4

35

Wikipedia articles may have Infobox templates. By the following call I can get the first section of an article which includes an Infobox.

http://en.wikipedia.org/w/api.php?action=parse&pageid=568801&section=0&prop=wikitext

I want a query which will return only Infobox data. Is this possible?

Converge answered 3/10, 2011 at 17:20 Comment(1)
See How to extract information from a Wikipedia infobox? for a more detailed answer.Luigi
I
38

You can do it with a URL call to the Wikipedia API like this:

http://en.wikipedia.org/w/api.php?action=query&prop=revisions&rvprop=content&format=xmlfm&titles=Scary%20Monsters%20and%20Nice%20Sprites&rvsection=0

Replace the titles= section with your page title, and format=xmlfm to format=json if you want the article in JSON format.

Inculcate answered 12/12, 2012 at 21:6 Comment(0)
O
17

Instead of parsing infoboxes yourself, which is quite complicated, take a look at DBPedia, which has Wikipedia infoboxes extracted out as database objects.

Ondrej answered 2/11, 2011 at 4:28 Comment(2)
This, however, will give you all the relationships to a particular entity but won't tell you which fields exist in the infoboxLozier
IIUIC they don't provide any databases via API, only some extraction tools. So you need to fetch everything locally.Eleventh
B
5

Building on garry's answer, you can have Wikipedia parse the info box into HTML for you via the rvparse parameter like so:

http://en.wikipedia.org/w/api.php?action=query&prop=revisions&rvprop=content&format=json&titles=Scary%20Monsters%20and%20Nice%20Sprites&rvsection=0&rvparse

Note that neither method will return just the info box. But from the HTML content, you can extract (via, e.g., Beautiful Soup) the table with class infobox.

In Python, you do something like the following

resp = requests.get(url).json()
page_one = next(iter(resp['query']['pages'].values()))
revisions = page_one.get('revisions', [])
html = next(iter(revisions[0].values()))
# Now parse the HTML 
Beelzebub answered 30/1, 2017 at 11:6 Comment(0)
S
4

If the page has a right side infobox, then use this URL to obtain it in txt form.

My example is using the element hydrogen. All you need to do is replace "Hydrogen" with your title.

https://en.wikipedia.org/w/index.php?action=raw&title=Template:Infobox%20hydrogen

If you are looking for JSON format use this URL, but it's not pretty.

https://en.wikipedia.org/w/api.php?action=parse&page=Template:Infobox%20hydrogen&format=json

Sadiras answered 25/5, 2017 at 12:49 Comment(1)
I think this works because this page exists: en.wikipedia.org/wiki/Template:Infobox_hydrogen . Replacing hydrogen alone with, let's say, "summer" doesn't workOvenbird

© 2022 - 2024 — McMap. All rights reserved.