-
-
Save geminorum/3183bc79d3be025896b1 to your computer and use it in GitHub Desktop.
call Wikipedia API using jQuery and parse result
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <div id="insertTest"></div> | |
| <script> | |
| var wikipediaHTMLResult = function(data) { | |
| var readData = $('<div>' + data.parse.text.* + '</div>'); | |
| // handle redirects | |
| var redirect = readData.find('li:contains("REDIRECT") a').text(); | |
| if(redirect != '') { | |
| callWikipediaAPI(redirect); | |
| return; | |
| } | |
| var box = readData.find('.infobox'); | |
| var binomialName = box.find('.binomial').text(); | |
| var fishName = box.find('th').first().text(); | |
| var imageURL = null; | |
| // Check if page has images | |
| if(data.parse.images.length >= 1) { | |
| imageURL = box.find('img').first().attr('src'); | |
| } | |
| $('#insertTest').append('<div><img src="'+ imageURL + '"/>'+ fishName +' <i>('+ binomialName +')</i></div>'); | |
| }; | |
| function callWikipediaAPI(wikipediaPage) { | |
| // http://www.mediawiki.org/wiki/API:Parsing_wikitext#parse | |
| $.getJSON('http://en.wikipedia.org/w/api.php?action=parse&format=json&callback=?', {page:wikipediaPage, prop:'text|images', uselang:'en'}, wikipediaHTMLResult); | |
| } | |
| callWikipediaAPI('Aholehole'); | |
| </script> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $.getJSON("http://en.wikipedia.org/w/api.php?action=parse&format=json&callback=?", {page:pageName, prop:"text"}, wikipediaHTMLResult); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $.getJSON("http://en.wikipedia.org/w/api.php?action=query&format=json&callback=?", {titles:pageName, prop: "images"}, wikipediaImageResult); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $.getJSON("http://en.wikipedia.org/w/api.php?action=query&format=json&callback=?", {titles:pageName, prop: "revisions", rvprop:"content"}, wikipediaPageResult); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment