Wikipedia API - get random page (s)

I am trying to get a JSON result with a set of random Wikipedia pages, including their names, content and images.

I played with my sandbox API, and so far the best I have is:

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

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

Does anyone know how?

Alternatively, I could use the title, content, and URL of one random page. The best I have is:

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

+9
source share
2 answers

You're close generator=random is the right way. Then you can use various prop values ​​to get the necessary information:

  • The page title is always on.
  • To get the text, use prop=revisons along with rvprop=content .
  • To get all the images used on the page, use prop=images .

    Please note that this often includes images that you probably are not interested in, such as icons and flags. To fix this, you can try prop=pageimages , although it does not seem to always work. Or you can try using both.

So, the final request might 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

+15
source

If you prefer to use their REST API,

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

Documentation

0
source

All Articles