HOW TO extract Wikipedia content through its API using jQuery


Wikipedia's API or more appropriately MediaWiki API,  lets you programmatically fetch content & metadata related to thousands of its articles.

I built a small custom Wikipedia search application - Q-Wikipedia, to get just the first paragraph of a Wikipedia article using jQuery with help from online resources.

The REST-based Wikipedia API has many configurable options. It's easy to select the options you require using the Wikipedia API Sandbox

As I need only the first paragraph I set the value of the sections parameter to 0 (default is all)

http://en.wikipedia.org/w/api.php?action=mobileview
&format=json&page="+ keyword + "
&redirect=no&sections=0&prop=text
&sectionprop=toclevel%7Clevel%7Cline%7Cnumber%7Cindex%7Cfromtitle%7Canchor
&callback=?

It has auto-completion of search terms, which was a breeze to implement using jQueryUI.

Super-script and a few other tags are removed from the Wikipedia content derived from the JSONP payload prior to injecting it into a DIV placeholder -

$('#wiki').html(json.mobileview.sections[0].text)
.find("dl,sup,.thumb,table.infobox,table.metadata,div.dablink").remove().end();

Try it out and let me know your feedback by posting a comment below.

Comments

  1. will you please provide the full source code for this...

    thanks . for sharing..

    ReplyDelete
  2. The JavaScript code to access the Wikipedia API is right in the HTML page source. Right click on the page & choose View Page Source to go through it.

    ReplyDelete

Post a Comment