Random article API call is returning User talk pages?
Asked Answered
H

2

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?

Hbeam answered 23/11, 2013 at 6:52 Comment(0)
S
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:

Swag answered 23/11, 2013 at 14:16 Comment(4)
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.Stiffler
Awesome! The API returned some warning about namespace but I didn't think that was the problem. Accepting this answer instead for future readers.Hbeam
What parameters should be used to get the extracts of more than one page via this query?Calabro
@VishnuKs grnlimit=max&exlimit=max&exintroSwag
S
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
Stiffler answered 23/11, 2013 at 7:22 Comment(1)
“The API does not allow you to directly get random pages” That's wrong, see my answer.Swag

© 2022 - 2024 — McMap. All rights reserved.