API:搜索
Appearance
本页是MediaWiki Action API帮助文档的一部份。 |
MediaWiki版本: | ≥ 1.11 |
GET请求通过标题或内容文本匹配来执行维基页面的高级搜索。
API帮助文档
GET请求
默认搜索结果没有UTF8编码。 在上面的get请求中替换
utf8
来查看区别。
回应
{
"batchcomplete": "",
"continue": {
"sroffset": 10,
"continue": "-||"
},
"query": {
"searchinfo": {
"totalhits": 5060
},
"search": [
{
"ns": 0,
"title": "Nelson Mandela",
"pageid": 21492751,
"size": 196026,
"wordcount": 23664,
"snippet": "<span class=\"searchmatch\">Nelson</span> Rolihlahla <span class=\"searchmatch\">Mandela</span> (/mænˈdɛlə/, Xhosa: [xoliɬaˈɬa <span class=\"searchmatch\">manˈdɛla</span>]; 18 July 1918 – 5 December 2013) was a South African anti-apartheid revolutionary,",
"timestamp": "2018-07-23T07:59:43Z"
},
{
"ns": 0,
"title": "Death of Nelson Mandela",
"pageid": 41284488,
"size": 133513,
"wordcount": 13512,
"snippet": "On December 5, 2013, <span class=\"searchmatch\">Nelson</span> <span class=\"searchmatch\">Mandela</span>, the first President of South Africa to be elected in a fully representative democratic election, as well as the country's",
"timestamp": "2018-07-19T17:30:59Z"
}
...
]
}
}
示例代码
Python
#!/usr/bin/python3
"""
search.py
MediaWiki API Demos
Demo of `Search` module: Search for a text or title
MIT License
"""
import requests
S = requests.Session()
URL = "https://en.wikipedia.org/w/api.php"
SEARCHPAGE = "Nelson Mandela"
PARAMS = {
"action": "query",
"format": "json",
"list": "search",
"srsearch": SEARCHPAGE
}
R = S.get(url=URL, params=PARAMS)
DATA = R.json()
if DATA['query']['search'][0]['title'] == SEARCHPAGE:
print("Your search page '" + SEARCHPAGE + "' exists on English Wikipedia")
PHP
<?php
/*
search.php
MediaWiki API Demos
Demo of `Search` module: Search for a text or title
MIT License
*/
$searchPage = "Nelson Mandela";
$endPoint = "https://en.wikipedia.org/w/api.php";
$params = [
"action" => "query",
"list" => "search",
"srsearch" => $searchPage,
"format" => "json"
];
$url = $endPoint . "?" . http_build_query( $params );
$ch = curl_init( $url );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
$output = curl_exec( $ch );
curl_close( $ch );
$result = json_decode( $output, true );
if ($result['query']['search'][0]['title'] == $searchPage){
echo("Your search page '" . $searchPage . "' exists on English Wikipedia" . "\n" );
}
JavaScript
/*
search.js
MediaWiki API Demos
Demo of `Search` module: Search for a text or title
MIT License
*/
var url = "https://en.wikipedia.org/w/api.php";
var params = new URLSearchParams({
action: "query",
list: "search",
srsearch: "Nelson Mandela",
format: "json",
origin: location.origin
});
fetch(`${url}?${params}`)
.then(function(response){return response.json();})
.then(function(response) {
if (response.query.search[0].title === "Nelson Mandela"){
console.log("Your search page 'Nelson Mandela' exists on English Wikipedia" );
}
})
.catch(function(error){console.log(error);});
MediaWiki JS
/*
search.js
MediaWiki API Demos
Demo of `Search` module: Search for a text or title
MIT License
*/
var params = {
action: 'query',
list: 'search',
srsearch: 'Nelson Mandela',
format: 'json'
},
api = new mw.Api();
api.get( params ).done( function ( data ) {
if ( data.query.search[ 0 ].title === 'Nelson Mandela' ) {
console.log( "Your search page 'Nelson Mandela' exists on English Wikipedia" );
}
} );
可能的错误
代码 | 信息 |
---|---|
nosrsearch | srsearch参数必须被设置。 这是1.17之前的参数搜索
|
search-text-disabled | text搜索已禁用。 |
search-title-disabled | title搜索已禁用。 |
search-error | 发生搜索错误 |
参数历史
- v1.24: 棄用
score
,hasrelated
- v1.23:
- 移除
srredirects
重定向始终包含在内。 - 启用
srinterwiki
- 移除
- v1.22: 启用
srbackend
- v1.17: 启用
nearmatch
,score
,titlesnippet
,redirecttitle
,redirectsnippet
,sectiontitle
,sectionsnippet
,hasrelated
- v1.16: 启用
srinfo
,srprop
額外說明
- 根据所使用的搜索后端的不同,
srsearch
的解释方式可能有所不同。 在使用CirrusSearch的维基媒体的维基上,有关搜索语法的信息,请参见Help:CirrusSearch 。