Tal Barda
Tal Barda

Reputation: 4277

How can I get a random article in a specific category from the Wikipedia API?

This is my link for getting one random article using Wiki API:

https://en.wikipedia.org/w/api.php?%20format=json&action=query&prop=extracts&exsentences=2&exintro=&explaintext=&generator=random&grnnamespace=0

I need to get from it the first two sentences of the first section, and it works pretty well.


I want to use this kind of link and search this random article in a specific category. This is what I have tried after searching online:

https://en.wikipedia.org/w/api.php?%20format=json&action=query&prop=extracts&exsentences=2&exintro=&explaintext=&generator=random&grnnamespace=0&cmtitle=Category:Music

(I have added this part to the original link: cmtitle=Category:Music )

It doesn't work for me. It gets the random article like the first link (not under a wanted category, which is Music in this link).

Upvotes: 2

Views: 4807

Answers (2)

Mon
Mon

Reputation: 3

try to use cmlimit to get all of the catgeorymembers, then use a programming language, like Python to request the page, then store every catgeory in an array, and use the random module to get a random catgeorymember from the array you stored them in. then you can use it in a link to get the specific page for the categorymember or anything else that you need.

Upvotes: 0

Tgr
Tgr

Reputation: 28160

There is no API to get a random category member (and using a parameter from some unrelated API module is certainly not going to help). You could screen scrape Special:RandomInCategory (or turn it into an API module - patches welcome :)

Upvotes: 2

Related Questions