如何检查维基百科上是否存在某个人?

问题描述 投票:0回答:1

所以我需要一个功能。一个示例输入将是“donald trump”(我不希望它区分大小写)如果Wikipedia上存在关于此人的文章,则函数返回摘要和图片。如果不返回false这是否可能?我无法弄清楚正确的API调用。

我试过过Wikidata API。它返回一些输入的多个对象,我不知道如何重定向它。

api wikipedia dbpedia wikidata wikidata-api
1个回答
1
投票

我创造了simple repo

你可以查看它在GitHub Page上的工作原理。

这是搜索功能:

$('#search').click(function (e) {
    articles1.empty();
    var search = $('#person').val();        
    console.log(search);
    var query = 'SELECT distinct ?image ?item ?itemLabel ?itemDescription ?DR ?RIP WHERE {?item wdt:P31 wd:Q5. ?item ?label "' + search + '"@en. OPTIONAL{?item wdt:P569 ?DR .} OPTIONAL{?item wdt:P570 ?RIP .} OPTIONAL{?item wdt:P18 ?image .} SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }  }';
    console.log(query);
    var queryUrl = encodeURI(url + "?query=" + query);
    $.ajax({
        dataType: "json",
        url: queryUrl,
        success: function (data) {                          
            var results = data.results.bindings;
            if (results.length > 0) {
                console.log(results);
                console.log(results[0].itemLabel.value);
                console.log(results[0].DR);
                console.log(results[0].RIP);

                for (var i = 0; i < results.length; i++) {
                    if (typeof (results[i].image) != 'undefined') {
                        var image = '<img class="inline" src=' + results[i].image.value + ' height="100">';
                        articles1.append(image);
                        articles1.append('<br>');
                    }
                    articles1.append(results[i].itemLabel.value);
                    articles1.append('<br>');
                    if (typeof (results[i].DR) != 'undefined') {
                        var dr = new Date(results[i].DR.value);                    
                        articles1.append(dr.getFullYear());
                        if (typeof (results[i].RIP) != 'undefined') {
                            var rip = new Date(results[i].RIP.value);
                            articles1.append(' - ');
                            articles1.append(rip.getFullYear());
                        }
                        articles1.append('<br>');
                    }
                    if (typeof (results[i].itemDescription) != 'undefined') {
                        articles1.append(results[i].itemDescription.value);
                    }
                    articles1.append('<br><br>');                        
                }
            }
            else {
                articles1.append("Sorry, no results");
            }
        },
        error: function (textStatus, errorThrown) {
            articles1.append("Sorry, no service");
        },
        timeout: 3000
    });
})
© www.soinside.com 2019 - 2024. All rights reserved.