维基百科的Javascript API提取数据 [英] Javascript extract data from Wikipedia API

查看:395
本文介绍了维基百科的Javascript API提取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何提取标题(按人口大都市的列表)维基百科API(<一个href=\"http://en.wikipedia.org/w/api.php?format=json&action=query&titles=List_of_metropolitan_areas_by_population&prop=revisions&rvprop=content&callback=\" rel=\"nofollow\">http://en.wikipedia.org/w/api.php?format=json&action=query&titles=List_of_metropolitan_areas_by_population&prop=revisions&rvprop=content&callback=?)

我是能够从维基百科获取数据,但是从中遇到了麻烦提取数据。

\r
\r

函数getJSONP(URL,成功){\r
    VAR UD ='_'+ +新的日期,\r
        脚本=使用document.createElement('脚本'),\r
        头= document.getElementsByTagName('头')[0] || document.documentElement中;\r
\r
    窗口[UD] =功能(数据){\r
        head.removeChild(脚本);\r
        成功和放大器;&安培;成功(数据);\r
    };\r
\r
    script.src = url.replace('回调=?','回调='+ UD);\r
    head.appendChild(脚本);\r
\r
}\r
getJSONP('http://en.wikipedia.org/w/api.php?format=json&action=query&titles=List_of_metropolitan_areas_by_population&prop=revisions&rvprop=content&callback=?',功能(数据){\r
    的console.log(数据);\r
    的document.getElementById(产出)的innerHTML = data.query。\r
});

\r

&LT; D​​IV ID =输出&GT;\r
    想在这里显示文章标题。\r
&LT; / DIV&GT;

\r

\r
\r

下面

是它在控制台中显示出来。我怎样才能提取从:List_of_metropolitan_areas_by_population,并在前端显示?


解决方案

您正试图使HTML元素的JavaScript对象,这就是为什么你看到你看到的结果的内容。你想要的是字符串,所以你需要访问会给你字符串对象的一部分。

 的document.getElementById(输出)的innerHTML = data.query.normalized [0]。从。

而不是

 的document.getElementById(输出)的innerHTML = data.query。

下面是小提琴的链接与你的答案附和:<​​a href=\"http://jsfiddle.net/zsaj950t/\" rel=\"nofollow\"> http://jsfiddle.net/zsaj950t/

How do I extract Title (List of metropolitan areas by population) from WIkipedia API(http://en.wikipedia.org/w/api.php?format=json&action=query&titles=List_of_metropolitan_areas_by_population&prop=revisions&rvprop=content&callback=?)

I was able to get data from Wikipedia but having trouble extract data from it.

function getJSONP(url, success) {
    var ud = '_' + +new Date,
        script = document.createElement('script'),
        head = document.getElementsByTagName('head')[0] || document.documentElement;

    window[ud] = function(data) {
        head.removeChild(script);
        success && success(data);
    };

    script.src = url.replace('callback=?', 'callback=' + ud);
    head.appendChild(script);

}
getJSONP('http://en.wikipedia.org/w/api.php?format=json&action=query&titles=List_of_metropolitan_areas_by_population&prop=revisions&rvprop=content&callback=?', function(data){
    console.log(data);
    document.getElementById("output").innerHTML = data.query;
}); 

<div id="output">
    Want to display article title here.
</div>

below is what it shows up in Console. How can I extract "from: "List_of_metropolitan_areas_by_population"" and display in front-end?

解决方案

You were trying to make the content of an HTML element a javascript object, which is why you saw the result you saw. What you want is the string, so you need to access the part of the object that will give you the string.

document.getElementById("output").innerHTML = data.query.normalized[0].from;

Instead of

document.getElementById("output").innerHTML = data.query;

Here's a link to the fiddle to go along with your answer: http://jsfiddle.net/zsaj950t/

这篇关于维基百科的Javascript API提取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆