Wikipedia API - 用于查找类别的生成器查询
Wikipedia API - Generator query to find categories
下面查询returns维基百科中列为'Good Articles'的文章(为题限制5篇)。
这工作正常,我得到以下结果:
{
"batchcomplete": "",
"continue": {
"eicontinue": "0|666",
"continue": "-||"
},
"query": {
"embeddedin": [
{
"pageid": 12,
"ns": 0,
"title": "Anarchism"
},
{
"pageid": 307,
"ns": 0,
"title": "Abraham Lincoln"
},
{
"pageid": 308,
"ns": 0,
"title": "Aristotle"
},
{
"pageid": 339,
"ns": 0,
"title": "Ayn Rand"
},
{
"pageid": 627,
"ns": 0,
"title": "Agriculture"
}
]
}
现在我想获取与每篇文章相关的类别,所以我尝试使用 'generator' 查询,如下所述:https://www.mediawiki.org/wiki/API:Query#Generators
我已经构建了这个 URL:
https://en.wikipedia.org/w/api.php?action=query&generator=embeddedin&geititle=Template:Good%20article&prop=categories
但由于某种原因,类别列表只显示第一个结果。
{
"continue": {
"clcontinue": "12|Articles_with_unsourced_statements_from_July_2019",
"continue": "||"
},
"query": {
"pages": {
"12": {
"pageid": 12,
"ns": 0,
"title": "Anarchism",
"categories": [
{
"ns": 14,
"title": "Category:All articles lacking reliable references"
},
{
"ns": 14,
"title": "Category:All articles with unsourced statements"
},
{
"ns": 14,
"title": "Category:Anarchism"
},
{
"ns": 14,
"title": "Category:Anti-capitalism"
},
{
"ns": 14,
"title": "Category:Anti-fascism"
},
{
"ns": 14,
"title": "Category:Articles containing French-language text"
},
{
"ns": 14,
"title": "Category:Articles containing Spanish-language text"
},
{
"ns": 14,
"title": "Category:Articles lacking reliable references from March 2019"
},
{
"ns": 14,
"title": "Category:Articles prone to spam from November 2014"
},
{
"ns": 14,
"title": "Category:Articles with short description"
}
]
},
"307": {
"pageid": 307,
"ns": 0,
"title": "Abraham Lincoln"
},
"308": {
"pageid": 308,
"ns": 0,
"title": "Aristotle"
},
"339": {
"pageid": 339,
"ns": 0,
"title": "Ayn Rand"
},
"627": {
"pageid": 627,
"ns": 0,
"title": "Agriculture"
},
"666": {
"pageid": 666,
"ns": 0,
"title": "Alkali metal"
},
"674": {
"pageid": 674,
"ns": 0,
"title": "Anatomy"
},
"736": {
"pageid": 736,
"ns": 0,
"title": "Albert Einstein"
},
"740": {
"pageid": 740,
"ns": 0,
"title": "Allah"
},
"746": {
"pageid": 746,
"ns": 0,
"title": "Azerbaijan"
}
}
}
我可以选择对第一个查询的结果使用 foreach 循环来获取所有相关类别,但我会发现从一个查询到 API 获得我需要的信息会更有效,如果可能的话。另外我想知道为什么只显示第一个结果。
感谢您的帮助!
即使这样也不能完全解决问题,类别太多,无法 return 在一个查询中全部解决。查看 query continuation 的文档了解如何处理(链接到旧版本,因为当前版本似乎没有清楚地解释这个概念)。
下面查询returns维基百科中列为'Good Articles'的文章(为题限制5篇)。
这工作正常,我得到以下结果:
{
"batchcomplete": "",
"continue": {
"eicontinue": "0|666",
"continue": "-||"
},
"query": {
"embeddedin": [
{
"pageid": 12,
"ns": 0,
"title": "Anarchism"
},
{
"pageid": 307,
"ns": 0,
"title": "Abraham Lincoln"
},
{
"pageid": 308,
"ns": 0,
"title": "Aristotle"
},
{
"pageid": 339,
"ns": 0,
"title": "Ayn Rand"
},
{
"pageid": 627,
"ns": 0,
"title": "Agriculture"
}
]
}
现在我想获取与每篇文章相关的类别,所以我尝试使用 'generator' 查询,如下所述:https://www.mediawiki.org/wiki/API:Query#Generators
我已经构建了这个 URL: https://en.wikipedia.org/w/api.php?action=query&generator=embeddedin&geititle=Template:Good%20article&prop=categories
但由于某种原因,类别列表只显示第一个结果。
{
"continue": {
"clcontinue": "12|Articles_with_unsourced_statements_from_July_2019",
"continue": "||"
},
"query": {
"pages": {
"12": {
"pageid": 12,
"ns": 0,
"title": "Anarchism",
"categories": [
{
"ns": 14,
"title": "Category:All articles lacking reliable references"
},
{
"ns": 14,
"title": "Category:All articles with unsourced statements"
},
{
"ns": 14,
"title": "Category:Anarchism"
},
{
"ns": 14,
"title": "Category:Anti-capitalism"
},
{
"ns": 14,
"title": "Category:Anti-fascism"
},
{
"ns": 14,
"title": "Category:Articles containing French-language text"
},
{
"ns": 14,
"title": "Category:Articles containing Spanish-language text"
},
{
"ns": 14,
"title": "Category:Articles lacking reliable references from March 2019"
},
{
"ns": 14,
"title": "Category:Articles prone to spam from November 2014"
},
{
"ns": 14,
"title": "Category:Articles with short description"
}
]
},
"307": {
"pageid": 307,
"ns": 0,
"title": "Abraham Lincoln"
},
"308": {
"pageid": 308,
"ns": 0,
"title": "Aristotle"
},
"339": {
"pageid": 339,
"ns": 0,
"title": "Ayn Rand"
},
"627": {
"pageid": 627,
"ns": 0,
"title": "Agriculture"
},
"666": {
"pageid": 666,
"ns": 0,
"title": "Alkali metal"
},
"674": {
"pageid": 674,
"ns": 0,
"title": "Anatomy"
},
"736": {
"pageid": 736,
"ns": 0,
"title": "Albert Einstein"
},
"740": {
"pageid": 740,
"ns": 0,
"title": "Allah"
},
"746": {
"pageid": 746,
"ns": 0,
"title": "Azerbaijan"
}
}
}
我可以选择对第一个查询的结果使用 foreach 循环来获取所有相关类别,但我会发现从一个查询到 API 获得我需要的信息会更有效,如果可能的话。另外我想知道为什么只显示第一个结果。
感谢您的帮助!
即使这样也不能完全解决问题,类别太多,无法 return 在一个查询中全部解决。查看 query continuation 的文档了解如何处理(链接到旧版本,因为当前版本似乎没有清楚地解释这个概念)。