Call Wikipedia API using jQuery
I decided to directly use the data from Wikipedia. Many pages contain a structured “Infobox” that I will use to gather information I need.
There is a Wikipedia API (more precisely, MediaWiki, the engine of Wikipedia, has an API). I invite you to read the documentation.
Here are some examples of what can be done using the javascript library jQuery:
- get the source of a page (API doc):
$.getJSON("http://en.wikipedia.org/w/api.php?action=query&format=json&callback=?", {titles:pageName, prop: "revisions", rvprop:"content"}, wikipediaPageResult);
- get the image names of a page (API doc):
$.getJSON("http://en.wikipedia.org/w/api.php?action=query&format=json&callback=?", {titles:pageName, prop: "images"}, wikipediaImageResult
- get the HTML formatted content of a page (API doc) (does not follow redirects)
$.getJSON("http://en.wikipedia.org/w/api.php?action=parse&format=json&callback=?", {page:pageName, prop:"text"}, wikipediaHTMLResult);
It is important to note the needed “&callback=?” in the query (it will tell jQuery to use JSONP, a way to do cross-site javascript call), thanks to stackoverflow for this tip.
Note that to get the HTML content of a wiki page using javascript from the browser, I cannot use the “action=render” parameter of index.php because of the Same Origin Policy. I had to use the API to do it client side. I think I will rewrite my system to do this call server-side.
In the end, check my gist on github to get the full code of how to import a Wikipedia Page in javascript.