Danyx
Danyx

Reputation: 724

How to get a list of titles and summaries from the Wikipedia API?

I've been trying to get a list of possible results (same you would get as you perform a search in Wikipedia) and a small summary of the article, usually the first paragraph.

So far all I can get is either the list of titles:

https://en.wikipedia.org/w/api.php?action=query&origin=*&list=search&srprop&srsearch=Albert%20Einstein&prop=extracts

or the summary for a single page:

https://en.wikipedia.org/w/api.php?action=query&prop=extracts&exintro=&explaintext=&titles=Albert%20Einstein

Is it possible to combine both these queries in a form similar to this

https://en.wikipedia.org/w/api.php?action=query&origin=*&list=search&srprop&srsearch=Albert%20Einstein&prop=extracts

or will I have to iterate all results from the first query and then get the extract for each one?

Upvotes: 2

Views: 1078

Answers (1)

Termininja
Termininja

Reputation: 7036

You can combine the results from two or more queries by using generator parameter. So the idea is to generate a list of search results (your first query) including extracts property for each one result (your second query):

action=query&generator=search&prop=extracts

Then we need to add some parameters for generator (all they prefixed with a "g")

gsrsearch=Albert%20Einstein&gsrlimit=20

and parameters for all query properties (in our case only for extracts):

exintro=1&explaintext=1&exchars=250&exlimit=20

The final query will be:

https://en.wikipedia.org/w/api.php?action=query&origin=*&generator=search&prop=extracts&gsrsearch=Albert%20Einstein&gsrlimit=20&exintro=1&explaintext=1&exchars=350&exlimit=20

Upvotes: 2

Related Questions