如何通过API在Wikimedia Commons获取图片说明及结构化数据?
Hey there, let's fix up your API requests to get exactly what you need—first that one-line image description, then the structured tags like "computer icon".
1. Getting the Image Description ("Add a one-line explanation...")
Your original request didn't return the description because you weren't targeting the right metadata field in extmetadata. Here are two straightforward fixes:
Option 1: Filter extmetadata to get the description directly
Modify your request to specify the ImageDescription field with iextmetadatafilter:
https://commons.wikimedia.org/w/api.php?action=query&titles=Image:Commons-logo.svg&prop=imageinfo&iiprop=extmetadata&iextmetadatafilter=ImageDescription
This narrows down the response to only the field you care about, and you'll find the one-line explanation under the ImageDescription entry.
Option 2: Grab the page's description directly
If you prefer a more direct path, use the info property with inprop=description:
https://commons.wikimedia.org/w/api.php?action=query&titles=Image:Commons-logo.svg&prop=info&inprop=description
Look for the description key in the response—this pulls the same text straight from the file page's metadata.
2. Fetching Structured Data like "computer icon"
Commons uses Wikibase to manage structured data for files, so you'll need to use the Wikibase API endpoints to get these tags. Here's the simplest way:
Use the wbgetentities endpoint
First, you'll need the file's Wikibase entity ID (for Commons-logo.svg, it's M42358—you can get this via a quick query request with prop=pageprops&ppprop=wikibase_item if you don't know it upfront). Then make this request:
https://commons.wikimedia.org/w/api.php?action=wbgetentities&ids=M42358&props=labels|claims&languages=en
In the response, look for the claims section under P31 (this is the "instance of" property). The linked entity's label will be your "computer icon" style tag—you can find it in the labels section of the response using the entity ID from the claim.
内容的提问来源于stack exchange,提问作者user5159718




