A couple of weeks ago while working on a project  I came across something very interesting, something called YQL ( Yahoo Query Language). YQL is a free service offered by Yahoo and it’s a very powerful tool for working with web services and doing all kind of interesting things like: data scraping, data insertion/updating and other cool and highly powerful things. YQL offers the possibility of creating different query’s over different web services and then retrieving the data in an easy to use XML or JSON format.

Here is how a YQL query looks like (this returns all flickr photos that contain “Cat” with a 10 limit) :

  1. select * from flickr.photos.search where text='Cat' limit 10

An other good functionality for YQL is data scraping, here is an example (this will find all ‘a’ tags from a specified xpath) :

  1. select * from html where url='http://example.com' and xpath='//body/p/a'

Ok. So now lets say that we want to add the data from the previous example in out page using jQuery.This is how it should be done:

  1. $.ajax(
  2.          {
  3.       type: 'GET',
  4.              url: "http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20html%20where%20url='http://example.com'%20
  5. and%20xpath='//body/p/a'&format=json",
  6.              dataType: 'jsonp',
  7.       success: function(data, textStatus)
  8.       {
  9.                 $('body').append(data.query.results.a.content);
  10.        }
  11. });

For help with creating your own YQL queries use the Yahoo YQL Console: http://developer.yahoo.com/yql/console/

For easy-to-use YQL queries i suggest using the YQL jQuery plugin : http://usingjquery.wordpress.com/jquery-yql-plugin/

After you install the YQL plugin this is how it can be used to replicate the example presented before:

  1. $.YQL("select * from  html where url='http://example.com' and xpath='//body/p/a'",function(content){
  2.           $('body').append(content.query.results.a.content);
  3. });