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
You're close generator=random is the right way. Then you can use various prop values ββto get the necessary information:
generator=random
prop
prop=revisons
rvprop=content
To get all the images used on the page, use prop=images .
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.
prop=pageimages
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
If you prefer to use their REST API,
curl -X GET "https://en.wikipedia.org/api/rest_v1/page/random/summary"
Documentation