How to check if a person exists on Wikipedia?

安稳与你 提交于 2019-12-11 17:22:52

问题


So I need a function. An example input would be like "donald trump" (I don't want it to be case-sensitive) If an article about the person exists on Wikipedia, function returns a summary and a picture. If not returns false Is this even possible? I couldn't figure out the correct API call.

I've tried Wikidata API. It returns multiple objects for some inputs and I didn't know how to redirect it.


回答1:


I created simple repo.

You can check how it work on GitHub Page.

This is search function:

$('#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
    });
})


来源:https://stackoverflow.com/questions/55409563/how-to-check-if-a-person-exists-on-wikipedia

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!