18

I'm trying to get a JSON result with a set of random pages from Wikipedia, including their titles, content and images.

I've played around with their API sandbox, and so far the best I've got is this:

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

But this only includes the namespace, id, and title of ten random pages. I would like to get the content as well as images as well.

Do anyone know how?

Alternatively I could do with the title, content and image url's of a single random page. Best I've got here is:

https://en.wikipedia.org/w/api.php?action=query&generator=random&format=json

svick
  • 236,525
  • 50
  • 385
  • 514
Petter
  • 773
  • 2
  • 9
  • 19

2 Answers2

20

You're close. generator=random is the right way to go. You can then use various prop values to get the info you want:

  • Page title is always included.

  • To get the text, use prop=revisons along with rvprop=content.

  • To get all images used on the page, use prop=images.

    Note that this will often include images you're probably not interested in, like icons and flags. To fix that, you might try instead prop=pageimages, though it doesn't seem to work always. Or you could try using both.

So, the final query could look like this:

https://en.wikipedia.org/w/api.php?format=json&action=query&generator=random&grnnamespace=0&prop=revisions|images&rvprop=content&grnlimit=10

Community
  • 1
  • 1
svick
  • 236,525
  • 50
  • 385
  • 514
  • What is grnnamespace and grnlimit? I was not able to find any documentation on them. – Yogeesh Seralathan Apr 23 '18 at 06:50
  • 2
    @YogeeshSeralathan See [the documentation for `rnnamespace` and `rnlimit`](https://www.mediawiki.org/wiki/API:Random). When used as a generator, the parameters are prefixed with `g`. – svick Apr 23 '18 at 09:59
  • in addition, it seems recent versions ask for a `&rvslots=main`, also – spencercooly Nov 17 '18 at 20:54
  • Once the API request is handled (many great suggestions above), what to do with the JSON data? Can that just be imported and parsed for display on the view? Thanks for any clarification. – Monroe Mann May 06 '20 at 21:49
11

If you'd rather use their REST api,

curl -X GET "https://en.wikipedia.org/api/rest_v1/page/random/summary"

Documentation

mancini0
  • 4,285
  • 1
  • 29
  • 31