问题
I'm studying Wikipedia REST API but I'm not able to find the right option to get also URLs for a search query.
this is the URL of the request:
http://it.wikipedia.org/w/api.php?action=query&list=search&srsearch=calvino&format=xml&srprop=snippet
this request outputs only the Title and the Snippet but no URLs for articles. I've checked wikipedia API documentation for the list=search query but seems there is no option to get also URLs.
Best Regards, Fabio Buda
回答1:
You can form the URL of the article easily by yourself from the title. For the Italian Wikipedia, it's http://it.wikipedia.org/wiki/
followed by the URL-encoded title of the article. It's as simple as that.
The actual URL of the article also replaces spaces with underscores, but you don't have to do that if you don't want to, the URL with spaces redirects to the one with underscores.
EDIT: You can get the URL, but it's not possible to get search-related information at the same time. To do that, use the list as a generator. For example:
http://it.wikipedia.org/w/api.php?action=query&generator=search&gsrsearch=calvino&format=xml&gsrprop=snippet&prop=info&inprop=url
But I think changing the format of page URLs is very unlikely: too many other people rely on that.
回答2:
I've found impossible to retrieve both description and url at once, so I split in two javascript method, the first get description, the second get url:
function get_wiki_info() {
$.ajax({
url: 'http://it.wikipedia.org/w/api.php',
data: { action: 'query', list: 'search', srsearch: $("input[name=city]").val(), format: 'json' },
dataType: 'jsonp',
success: function (data) {
console.log('wiki', data.query.search[0].snippet);
$('#info-wiki-text').html(data.query.search[0].snippet);
get_wiki_links();
},
fail: function (data) {
$('#info-wiki-text').html("Impossible retrieve information for " + $("input[name=city]").val());
}
});
}
function get_wiki_links() {
$.ajax({
url: 'http://it.wikipedia.org/w/api.php',
data: { action: 'query', generator: 'allpages', search: $("input[name=city]").val(), format: 'json', gapfrom: $("input[name=city]").val(), gapto: $("input[name=city]").val(), prop: 'info', inprop: 'url' },
dataType: 'jsonp',
success: function (data) {
console.log('wiki', data.query.pages);
$.each(data.query.pages, function (key, val) {
$('#wiki-city-link').attr('href', val.fullurl);
});
},
fail: function (data) {
console.log(data);
}
});
}
If you prefer, to retrieve description:
https://it.wikipedia.org/w/api.php?action=query&list=search&srsearch=Your%20Params&utf8=
to retrieve url:
https://it.wikipedia.org/w/api.php?action=query&generator=allpages&search=Your%20Params&gapfrom=Your%20Params&gapto=Your%20Params&prop=info&inprop=url&utf8=
来源:https://stackoverflow.com/questions/8930867/wikipedia-list-search-rest-api-how-to-retrieve-also-url-of-matching-articles