API:Query/zh
![]() |
action=query模組允许你取得有關維基和其中保存資料的資訊,例如特定頁面的維基文本,一組頁面的連結和分類,或是更改維基內容 所需要的存取權杖。
查詢模組
查詢模組有三種子模块(也被稱為查詢模組):
範例1:指定頁面
与元数据和列表查询模块不同,所有属性查询模块都在一组页面上工作,这些页面可以通过以下方式之一进行指定:
- 按名称,使用参数
titles
,例如titles=Foo|Bar|Main_Page
。 - 按页面ID,使用参数
pageids
,例如pageids=123|456|75915
。 - 按修订版本ID,使用
revids
参数,例如revids=478198|54872|54894545
。 大多数查询模块会将修订ID转换为相应的页面ID。 只有prop=revisions 实际上使用了修订ID本身。 - 使用一个发生器 Category:Gadget:TabbedWindow
GET请求
回應
{
"batchcomplete": true,
"query": {
"pages": [
{
"pageid": 1130,
"ns": 0,
"title": "Avicenna"
},
{
"pageid": 17412,
"ns": 0,
"title": "Klein bottle"
},
{
"pageid": 33642,
"ns": 0,
"title": "Warrant"
}
]
}
}
范例2:标题规范化
标题规范化将页面标题转换为规范形式。
这意味着将首字母大写,将下划线替换为空格,并将命名空间更改为该wiki定义的本地化形式。
GET请求
回應
{
"batchcomplete": true,
"query": {
"normalized": [
{
"fromencoded": false,
"from": "Project:articleA",
"to": "Wikipedia:ArticleA"
},
{
"fromencoded": false,
"from": "article_B",
"to": "Article B"
}
],
"pages": [
{
"ns": 0,
"title": "Article B",
"missing": true
},
{
"ns": 4,
"title": "Wikipedia:ArticleA",
"missing": true
}
]
}
}
範例3:不存在和無效標題
不存在或无效的标题将在响应中设置missing
或invalid
属性。
在支持数字数组键的输出格式中,缺失和无效的标题将具有负页面ID。
在某些情况下,用户可以看到标题,但无法通过API访问,例如内容镜像自其他wiki的页面。
这些标题将在响应中设置 known
属性。
GET请求
回應
{
"batchcomplete": true,
"query": {
"pages": [
{
"ns": 0,
"title": "Doesntexist",
"missing": true
},
{
"title": "Talk:",
"invalidreason": "The requested page title is empty or contains only the name of a namespace.",
"invalid": true
},
{
"pageid": 15580374,
"ns": 0,
"title": "Main Page"
}
]
}
}
範例4:繼續查詢
当查询的响应中没有返回所有数据时,将会有一个 continue
属性来指示还有更多数据。
GET请求
回應
{
"continue": {
"accontinue": "List_of_largest_companies_in_Sri_Lanka",
"continue": "-||"
},
"query": {
"allcategories": [
{
"category": "List of BioWare characters"
},
{
"category": "List of Harlequin Romance novels"
},
{
"category": "List of MPs elected in UK elections templates"
},
{
"category": "List of Metamorphoses characters"
},
{
"category": "List of Rockstar Games characters"
},
{
"category": "List of Star Trek awards and nominations"
},
{
"category": "List of Swedish films of the 2020s"
},
{
"category": "List of association football clubs in the Republic of Ireland templates"
},
{
"category": "List of awards and nominations received by Aleksej Pechkuroy"
},
{
"category": "List of cabinet templates"
}
]
}
}
To get further data, add its values to the original request:
GET请求
回應
{
"batchcomplete": true,
"query": {
"allcategories": [
{
"category": "List of largest companies in Sri Lanka"
},
{
"category": "List of longest beaches of the world"
},
{
"category": "List of ministers by ministry of Bangladesh"
},
{
"category": "List of people from Palm Beach, Florida"
},
{
"category": "List of video game characters"
}
]
}
}
示例 5:批处理
API返回一个batchcomplete
元素,以指示已返回当前批次项目的所有数据。
MW 1.25+
在下面的示例查询响应中,已包含batchcomplete
以表示已返回三张图片中的所有数据。
下一部分将继续为下一组3张图片返回数据。
Category:Gadget:TabbedWindow
GET请求
回應
{
"batchcomplete": true,
"continue": {
"aicontinue": "20020822143445|Do_You_Want_to_Know_a_Secret_(Beatles_song_-_sample).ogg",
"continue": "-||"
},
"query": {
"allimages": [
{
"name": "Simon_and_Garfunkel_-_Mrs_Robinson.ogg",
"timestamp": "2002-08-04T19:55:17Z",
"url": "https://upload.wikimedia.org/wikipedia/en/6/64/Simon_and_Garfunkel_-_Mrs_Robinson.ogg",
"descriptionurl": "https://en.wikipedia.org/wiki/File:Simon_and_Garfunkel_-_Mrs_Robinson.ogg",
"descriptionshorturl": "https://en.wikipedia.org/w/index.php?curid=67723",
"ns": 6,
"title": "File:Simon and Garfunkel - Mrs Robinson.ogg"
},
{
"name": "Simon_and_Garfunkel_-_Scarborough_Fair.ogg",
"timestamp": "2002-08-04T20:01:36Z",
"url": "https://upload.wikimedia.org/wikipedia/en/c/c1/Simon_and_Garfunkel_-_Scarborough_Fair.ogg",
"descriptionurl": "https://en.wikipedia.org/wiki/File:Simon_and_Garfunkel_-_Scarborough_Fair.ogg",
"descriptionshorturl": "https://en.wikipedia.org/w/index.php?curid=67779",
"ns": 6,
"title": "File:Simon and Garfunkel - Scarborough Fair.ogg"
},
{
"name": "Beatles_please_me.ogg",
"timestamp": "2002-08-22T14:34:00Z",
"url": "https://upload.wikimedia.org/wikipedia/en/e/ee/Beatles_please_me.ogg",
"descriptionurl": "https://en.wikipedia.org/wiki/File:Beatles_please_me.ogg",
"descriptionshorturl": "https://en.wikipedia.org/w/index.php?curid=74826",
"ns": 6,
"title": "File:Beatles please me.ogg"
}
]
}
}
示例 6:生成器
使用生成器,若您希望获取一组页面的数据。
例如,要获取某个分类中页面的数据,可以使用generator=categorymembers
将两个API调用合并为一个,以取代先查询list=categorymembers
,再将pageids
设置为对所有返回的页面进行查询的操作。
当使用列表模块作为生成器时,您不需要指定页码。
然而,对于一个属性模块,您应该指定生成器将工作的页面。
例如,要加载从主页链接的所有页面,请使用generator=links&titles=Main%20Page
。
传递给生成器的参数必须以 g
为前缀。例如,当使用 generator=backlinks
时,应使用 gbllimit
而不是 bllimit
。
下面的示例查询获取了主命名空间中以“Ba”开头的前三页的链接和分类。
Category:Gadget:TabbedWindow
GET请求
回應
{
"continue": {
"plcontinue": "14977970|0|Kirkwall_Ba_game",
"continue": "||categories"
},
"query": {
"pages": [
{
"pageid": 98178,
"ns": 0,
"title": "Ba",
"links": [
{
"ns": 0,
"title": "BA"
},
{
"ns": 4,
"title": "Wikipedia:Mainspace"
},
{
"ns": 4,
"title": "Wikipedia:Naming conventions (capitalization)"
},
{
"ns": 4,
"title": "Wikipedia:Protection policy"
},
{
"ns": 4,
"title": "Wikipedia:Redirect"
},
{
"ns": 10,
"title": "Template:R from miscapitalisation"
},
{
"ns": 10,
"title": "Template:R from modification"
},
{
"ns": 10,
"title": "Template:R to disambiguation page"
},
{
"ns": 14,
"title": "Category:Redirects from ambiguous terms"
},
{
"ns": 14,
"title": "Category:Redirects from other capitalisations"
}
],
"categories": [
{
"ns": 14,
"title": "Category:Redirects from ambiguous terms"
},
{
"ns": 14,
"title": "Category:Redirects from other capitalisations"
},
{
"ns": 14,
"title": "Category:Unprintworthy redirects"
}
]
},
{
"pageid": 14977970,
"ns": 0,
"title": "Ba'"
},
{
"pageid": 33351890,
"ns": 0,
"title": "Ba'Al Shem Tov"
}
]
}
}
继续查询
查询通常会显示比原始查询更多的结果。 (Often this is because a List query's result limit has been reached.) 在这些情况下,可以继续查询。 关于继续查询的更详细信息可以在API:Continue 处找到。
可能的警告
警告訊息 | 原因 |
---|---|
No support for special pages has been implemented. | 如果给出Special:或Media:命名空间中的标题,则会抛出异常。这些命名空间中的页面无法查询。 |
重定向解决方案不能与revids参数一起使用。任何revids所指向的重定向都未被解决。 | 如果查询中使用了redirect 参数,并且指定了使用revids 的页面,则会抛出异常。 |
参数历史
- v1.34: 启用
exportschema
- v1.24: 啟用
rawcontinue
(note: raw continuation was the default behavior until v1.26) - v1.21: 啟用
continue
附加提醒
- 通过
titles
或pageids
指定标题,每个查询限制为50个标题,对于有apihighlimits
权限的人则限制为500个。 - 将多个查询模块一起使用,以在一次请求中获取所需内容,例如
prop=info|revisions&list=backlinks|embeddedin|allimages&meta=userinfo
。 - 生成器总是将页面标题传递给查询模块。 与列表(默认可能包含额外数据)不同,生成器本身不应输出任何信息,除非通过生成器模块的查询参数明确请求。
Resolving redirects
重定向可以自动解决,因此返回的是重定向的目标,而不是给定的标题。
当存在时,它们将始终包含from
和to
属性,并且可能包含一个tofragment
属性,用于指向特定章节的那些重定向。
规范化和重定向都可能发生。
在存在多个重定向的情况下,所有重定向都将被解决,并且在存在循环重定向的情况下,“pages”部分可能没有页面(参见一个示例请求)。
重定向解析不能与revids=
参数或生成revids的生成器一起使用;这样做将产生一个警告,并且不会解析指定的revids的重定向。
下面的例子展示了redirects
参数的工作方式。
结果 |
---|
{
"batchcomplete": true,
"query": {
"redirects": [
{
"from": "Main page",
"to": "Main Page"
}
],
"pages": [
{
"pageid": 15580374,
"ns": 0,
"title": "Main Page"
}
]
}
}
|
结果 |
---|
{
"batchcomplete": true,
"query": {
"pages": [
{
"pageid": 217225,
"ns": 0,
"title": "Main page"
}
]
}
}
|
结果 |
---|
{
"batchcomplete": true,
"query": {
"pages": [
{
"pageid": 217225,
"ns": 0,
"title": "Main page",
"contentmodel": "wikitext",
"pagelanguage": "en",
"pagelanguagehtmlcode": "en",
"pagelanguagedir": "ltr",
"touched": "2017-12-17T02:02:05Z",
"lastrevid": 777924062,
"length": 170,
"redirect": true
}
]
}
}
|
结果 |
---|
{
"batchcomplete": true,
"query": {
"redirects": [
{
"from": "Wikipedia:!--",
"to": "Wikipedia:Manual of Style",
"tofragment": "Invisible comments"
}
],
"pages": [
{
"pageid": 33697,
"ns": 4,
"title": "Wikipedia:Manual of Style"
}
]
}
}
|
结果 |
---|
{
"batchcomplete": true,
"query": {
"normalized": [
{
"fromencoded": false,
"from": "page1",
"to": "Page1"
}
],
"redirects": [
{
"from": "Page1",
"to": "Page2"
},
{
"from": "Page2",
"to": "Page3"
},
{
"from": "Page3",
"to": "Page1"
}
],
"pages": [
{
"ns": 0,
"title": "Page1",
"missing": true
}
]
}
}
|
參見
- API: - 快速上手指南
- API:Raw query continue - 包含有关如何使用
rawcontinue
参数的信息。
- Maintained by MediaWiki Interfaces Team.
- Live chat (IRC): #mediawiki-core
- Issue tracker: Phabricator MediaWiki-Action-API (Report an issue)