Retrieving Wikipedia InfoBox Content Using JQuery

I want to use jQuery to display the contents of a Wikipedia info box containing company information.

I think I'm almost there, but I just can't take the last step on the road

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

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

This contains a table with the contents of the info box:

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

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

It works with some other page elements - for example, swapping.infobox for .logo works fine.

I am pleased to provide more information, but I spent many hours on it and tried so many permutations that I'm not even sure what is relevant ...

TIA

+6
source share
1 answer

Wikipedia JSON does not seem to return a wrapper document element. This, apparently, prevents the choice of any attributes of the elements located in the root. Try the following:

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

Hope it works!

+5
source

Source: https://habr.com/ru/post/904129/


All Articles