使用JQuery获取Wikipedia信息框内容 [英] Getting Wikipedia infobox content with JQuery

查看:92
本文介绍了使用JQuery获取Wikipedia信息框内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找使用JQuery来拉回包含公司详细信息的Wikipedia信息框的内容.

I'm looking to use JQuery to pull back contents of the Wikipedia infobox that contains company details.

我认为我快到了,但是我走不了最后一步

I think that I'm almost there but I just can't get the last step of the way

var searchTerm="toyota";
var url="http://en.wikipedia.org/w/api.php?action=parse&format=json&page=" + searchTerm+"&redirects&prop=text&callback=?";
$.getJSON(url,function(data){
  wikiHTML = data.parse.text["*"];
  $wikiDOM = $(wikiHTML);
  $("#result").append($wikiDOM.find('.infobox').html());
});

第一部分有效-WikiHTML包含页面内容,由Wikipedia API解析为HTML格式

The first part works - wikiHTML contains the content of the page, parsed by the Wikipedia API to HTML format

这包含带有信息框内容的表:

This contains the table with the infobox content:

 <table class="infobox vcard" cellspacing="5" style="width:22em;">

结果只是一个空表占位符,用于将数据放入

result is just an empty table placeholder to put the data in

它可以与页面中的其他一些元素一起使用-例如,将.infobox替换为.logo可以很好地工作.

It works with some other elements from the page - for example, swapping .infobox for .logo works perfectly.

很乐意提供更多信息,但是我已经花了几个小时在此上,并且尝试了很多排列,以至于我什至不确定什么是相关的...

Happy to provide more info, but I've spent hours on this and tried so many permutations that I'm not even sure what's relevant anymore...

TIA

推荐答案

似乎Wikipedia的JSON不返回包装文档元素.这似乎阻止了位于根元素上的任何属性的选择.试试这个:

It seems Wikipedia's JSON doesn't return a wrapping document element. This appears to be preventing any attributes on the elements that are at the root from being selectable. Try this:

var searchTerm="toyota";
var url="http://en.wikipedia.org/w/api.php?action=parse&format=json&page=" + searchTerm+"&redirects&prop=text&callback=?";
$.getJSON(url,function(data){
  wikiHTML = data.parse.text["*"];
  $wikiDOM = $("<document>"+wikiHTML+"</document>");
  $("#result").append($wikiDOM.find('.infobox').html());
});

希望有效!

这篇关于使用JQuery获取Wikipedia信息框内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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