9

I'm trying to pull a random article from the WikiMedia API, but my code seems to only grab User talk pages...

$(document).ready(function(){
  $.getJSON("http://en.wikipedia.org/w/api.php?action=query&generator=random&prop=extracts&exchars=500&format=json&callback=?", function (data) {
    console.log(data.query.pages);
  });
});

I read that "generator=random" pulls a random article, but that does not seem to be the case. How do I get it working as intended?

Damjan Pavlica
  • 31,277
  • 10
  • 71
  • 76
theintellects
  • 1,320
  • 2
  • 16
  • 28

2 Answers2

14

If you want to get only pages in namespace 0, you need to specify the rnnamespace parameter. And since you're using list=random as a generator, it's spelled as grnnamespace:

Ilmari Karonen
  • 49,047
  • 9
  • 93
  • 153
svick
  • 236,525
  • 50
  • 385
  • 514
  • 1
    That's quite ingenious... I did not find that in the MediaWiki API reference. I've removed my comment that it was impossible and linked your answer. – Kevin Ji Nov 23 '13 at 17:49
  • Awesome! The API returned some warning about namespace but I didn't think that was the problem. Accepting this answer instead for future readers. – theintellects Nov 23 '13 at 19:30
  • What parameters should be used to get the extracts of more than one page via this query? – Vishnu Ks Mar 16 '15 at 22:06
  • 1
    @VishnuKs `grnlimit=max&exlimit=max&exintro` – svick Mar 16 '15 at 22:52
1

The API does not allow you to directly get random pages; the random generator currently gets random pages from any namespace. EDIT: I stand corrected; apparently you can by passing in a grnamespace parameter, as svick mentions in their answer. I'll leave my original answer below though.

How about making two API calls?

First, make a call to grab a list of random pages:

https://en.wikipedia.org/w/api.php?action=query&list=random&rnnamespace=0&rnlimit=5&format=json

Adjust the rnlimit parameter based on how many pages you want.

To get the wiki-source, use the following (replacing TITLE1, TITLE2, etc. with your actual titles):

https://en.wikipedia.org/w/api.php?action=query&titles=TITLE1|TITLE2&prop=revisions&rvprop=content&format=json

For an HTML copy of the pages, use the following (replacing TITLE with your actual title, and calling the API repeatedly):

https://en.wikipedia.org/w/api.php?action=parse&page=TITLE&prop=text&format=json

Of course, it might just be easier to call Special:Random directly, and screen-scrape:

https://en.wikipedia.org/wiki/Special:Random
Community
  • 1
  • 1
Kevin Ji
  • 10,479
  • 4
  • 40
  • 63
  • 3
    “The API does not allow you to directly get random pages” That's wrong, see my answer. – svick Nov 23 '13 at 14:17