Wikipedia list=search REST API: how to retrieve also Url of matching articles
Asked Answered
J

2

12

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

Jammiejammin answered 19/1, 2012 at 17:52 Comment(0)
B
23

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.

Bulwerlytton answered 19/1, 2012 at 18:39 Comment(2)
You're right but I expected URLs in the XML|JSON response: what if wikipedia will change URL structure (it.wiki.../wiki/) in future? having URLs directly from wikipedia API guarantee url consistency. But no problem and thanks for your answer.Jammiejammin
Actually, it's not just URL encoding. Compare: en.wikipedia.org/wiki/пиетет to its URL-encoded counterpart:Typeface
D
0

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=

Domestic answered 14/8, 2017 at 9:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.