I'm trying to retrieve a list of urls of images in Wikimedia Commons from a particular article. I want to either search article by category and retrieve images from them, or filter images by category. I've tried including incategory:category_name in the titles string of images and article searches as well as [[Category:category%20name]] and Category:category_name to no avail. I'm given the impression it's possible, and the advanced search functionality within Wikipedia would imply so, but how to achieve this through the API is not clear.

Specifically, I'm interested in grabbing these images through the Mediawiki API.

up vote 0 down vote accepted

Yes, Wikimedia Commons exposes MediaWiki API.

Basically, your query should be the following:

The API:Categorymembers module can be used as a generator.

If you need real URLs of images (not their File: titles), pass results into the API:Imageinfo module:

  • https://commons.wikimedia.org/w/api.php
    ?action=query
    &generator=categorymembers
    &gcmtitle=Category:Watercolor%20paintings
    &gcmlimit=100
    &gcmtype=file
    &prop=imageinfo
    &iiprop=url
    &format=json

    "query": {
        "pages": {
          "52082165": {
            "pageid": 52082165,
            "ns": 6,
            "title": "File:\"Flying World\".jpg",
            "imagerepository": "local",
            "imageinfo": [
              {
                "url": "https://upload.wikimedia.org/wikipedia/commons/7/7e/%22Flying_World%22.jpg",
                "descriptionurl": "https://commons.wikimedia.org/wiki/File:%22Flying_World%22.jpg",
                "descriptionshorturl": "https://commons.wikimedia.org/w/index.php?curid=52082165"
              }
            ]
          },
          "52082336": {
            "pageid": 52082336,
            "ns": 6,
            "title": "File:\"Before-After the Fall\".jpg",
            "imagerepository": "local",
            "imageinfo": [
              {
                "url": "https://upload.wikimedia.org/wikipedia/commons/2/24/%22Before-After_the_Fall%22.jpg",
                "descriptionurl": "https://commons.wikimedia.org/wiki/File:%22Before-After_the_Fall%22.jpg",
                "descriptionshorturl": "https://commons.wikimedia.org/w/index.php?curid=52082336"
              }
            ]
          }
        }
      }
    

If you need all 1,723 images (which is more than 500), read about generators and continuation.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.