2

There is a property called extracts which gives you the summary for WikiPedia pages. It can be obtained by adding &prop=extracts in the url call. I am accessing an array of results, and I observe that only few results has the extract and the rest don't. It is usually either the first or the last result.

Here is the sample code

$.getJSON("https://en.wikipedia.org/w/api.php?action=query&format=json&prop=extracts&gsrlimit=15&generator=search&origin=*&gsrsearch=" + searchTerm, function(data){
        console.log(data);
        $.each(data.query.pages, function (i) {
        console.log(data.query.pages[i].extract);
});
      });

Here is a screenshot of the console results Part of the Console Result

As you can see the extract property is missing for all the search results except the first one. Why is it so? And how do I correct this issue?

Okay so after referring to this question and the documentation. I made this call

$.getJSON("https://en.wikipedia.org/w/api.php?action=query&format=json&prop=extracts&exintro=1&excontinue=1&exlimit=max&gsrlimit=10&generator=search&origin=*&gsrsearch=" + searchTerm, function(data){
console.log(data);
    $.each(data.query.pages, function (i) {
  console.log(data.query.pages[i].extract);});});  

Now the extracts for all the results are shown except the first and the last one.

Community
  • 1
  • 1
djokester
  • 567
  • 9
  • 20
  • 1
    Have you seen this? http://stackoverflow.com/questions/9846795/prop-extracts-not-returning-all-extracts-in-the-wikimedia-api – Maria Ines Parnisari May 01 '17 at 01:08
  • Tried the solution out, doesn't work at all. I read up the documentation to get an idea and updated the question. – djokester May 01 '17 at 01:30
  • Query continuation works differently these days but that's indeed the problem. See [docs](https://www.mediawiki.org/wiki/API:Query#Continuing_queries). – Tgr May 04 '17 at 09:15

1 Answers1

0

While clicking around in the Wikipedia API sandbox, I got the text extracts to work by adding the exintro=1 property to the query.

So in your example, the code would be

$.getJSON("https://en.wikipedia.org/w/api.php?action=query&format=json&prop=extracts&generator=search&redirects=1&formatversion=latest&exintro=1&origin=*&gsrsearch=" + searchTerm, function(data){
    console.log(data);
    $.each(data.query.pages, function (i) {
    console.log(data.query.pages[i].extract);
    });
});

Here's a link to the API sandbox version of the query. There are also options to limit the length of the extract, and explanations for what the properties do, etc.

https://en.wikipedia.org/wiki/Special:ApiSandbox#action=query&format=json&prop=extracts&generator=search&redirects=1&formatversion=latest&exintro=1&gsrsearch=pants

Jan Drewniak
  • 1,316
  • 15
  • 18