我需要在任何给定的 wiki 页面中获取第一个图像/主图像。我可以使用抓取工具来做到这一点。但我正在使用 curl 来废弃页面。但可能是由于互联网连接速度较慢,仅废弃一个 wiki 页面需要很长时间。除此之外,我需要根据用户的查询同时显示至少 7-8 个不同的 wiki 图像。
因此,为此使用 curl 毫无意义。 我试过wiki api
https://en.wikipedia.org/w/api.php?action=query&titles=India&prop=images&imlimit=1
但是我没有其他参数可以给这个列表排序。通常,此 api 返回的第一张图片不是您在页面顶部看到的主图片。有时图像与页面上下文的距离太远。
我只需要为每个 wiki 标题显示一张图片。提前致谢。
最佳答案
要经常很好地猜测“主图像”,请使用 prop=pageimages
,由 MediaWiki extension "PageImages" 提供:
The PageImages extension collects information about images used on a page.
Its aim is to return the single most appropriate thumbnail associated with an article, attempting to return only meaningful images, e.g. not those from maintenance templates, stubs or flag icons. Currently it uses the first non-meaningless image used in the page.
(Text is cc-by-sa 3.0; list of authors)
用法
引用 MediaWiki API documentation :
Returns information about images on the page, such as thumbnail and presence of photos. Parameters: piprop Which information to return: thumbnail URL and dimensions of image associated with page, if any. name Image title. Values (separate with "|"): thumbnail, name Default: thumbnail|name pithumbsize Maximum thumbnail dimension. Default: 50 pilimit Properties of how many pages to return. No more than 50 (100 for bots) allowed. Default: 1 picontinue When more results are available, use this to continue.
Example
https://en.wikipedia.org/w/api.php?action=query&titles=India&prop=pageimages&pithumbsize=300
Return value:
{
"query": {
"pages": {
"14533": {
"pageid": 14533,
"ns": 0,
"title": "India",
"thumbnail": {
"source": "https://upload.wikimedia.org/wikipedia/commons/thumb/b/b8/Political_map_of_India_EN.svg/256px-Political_map_of_India_EN.svg.png",
"width": 256,
"height": 300
},
"pageimage": "Political_map_of_India_EN.svg"
}
}
}
}
更多示例:
关于php - 如何获取任何 wiki 页面的第一张图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10248475/