Wikipedia list=search REST API:如何检索匹配文章的 Url

发布于 2024-12-28 01:37:15 字数 323 浏览 1 评论 0原文

我正在研究维基百科 REST API,但我无法找到正确的选项来获取搜索查询的 URL。

这是请求的 URL:

http://it.wikipedia.org/w/api.php?action=query&list=search&srsearch=calvino&format=xml&srprop=snippet

此请求仅输出标题和片段,但不输出文章的 URL。 我已经检查了 wikipedia API 文档中的 list=search 查询,但似乎没有选项可以获取 URL。

此致, 法比奥·布达

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

空心空情空意 2025-01-04 01:37:15

您可以根据标题轻松地自行形成文章的 URL。对于意大利语维基百科,它是 http://it.wikipedia.org/wiki/ 后跟 URL 编码的文章标题。就这么简单。

文章的实际 URL 也将空格替换为下划线,但如果您不想,则不必这样做,带空格的 URL 会重定向到带下划线的 URL。

编辑:您可以获取 URL,但无法同时获取搜索相关信息。为此,请将该列表用作生成器。例如:

http://it.wikipedia.org/w/api.php?action=query&generator=search&gsrsearch=calvino&format=xml&gsrprop=snippet&prop=info&inprop=url

但我认为更改页面 URL 的格式不太可能:太多其他人依赖于此。

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.

胡渣熟男 2025-01-04 01:37:15

我发现不可能同时检索描述和 url,所以我分成两个 javascript 方法,第一个获取描述,第二个获取 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);
        }
    });
}

如果您愿意,检索描述:

https://it.wikipedia.org/w/api.php?action=query&list=search&srsearch=Your%20Params&utf8=

检索网址:

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=

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=

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文