How to use YQL to get web results?

It's hard for me to create a simple html file using javascript to display the results of a YQL query.

I understand how to configure the select statement (example: select a title, abstract, url from search.web, where query = "pizza") in the YQL console. But I do not know how to display it in an html file?

Does anyone help explain how to display the results of this statement? Code snippets will be appreciated!

By the way, I read YQL Docs, but they are somewhat complicated.

+4
source share
2 answers

The only way to get YQL results through client-side JavaScript is through JSON-P (or using an optional proxy server). Here is the wrapper for the YQL service:

function YQLQuery(query, callback) { this.query = query; this.callback = callback || function(){}; this.fetch = function() { if (!this.query || !this.callback) { throw new Error('YQLQuery.fetch(): Parameters may be undefined'); } var scriptEl = document.createElement('script'), uid = 'yql' + +new Date(), encodedQuery = encodeURIComponent(this.query.toLowerCase()), instance = this; YQLQuery[uid] = function(json) { instance.callback(json); delete YQLQuery[uid]; document.body.removeChild(scriptEl); }; scriptEl.src = 'http://query.yahooapis.com/v1/public/yql?q=' + encodedQuery + '&format=json&callback=YQLQuery.' + uid; document.body.appendChild(scriptEl); }; } 

Application:

 // Construct your query: var query = "select * from rss where url='somefeed.com' limit 1"; // Define your callback: var callback = function(data) { var post = data.query.results.item; alert(post.title); }; // Instantiate with the query: var firstFeedItem = new YQLQuery(query, callback); // If you're ready then go: firstFeedItem.fetch(); // Go!! 

Additional information : http://james.padolsey.com/javascript/using-yql-with-jsonp/

+8
source

Here is a very simple example. I did this using the YQL website:

 <html> <head> </head> <body> <script> function top_stories(o){ // parse through the output here: var items = o.query.results.item; // do whatever you want with the output here: console.log(items[0].title); } </script> <script src='http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20rss%20where%20url%3D%22http%3A%2F%2Frss.news.yahoo.com%2Frss%2Ftopstories%22&format=json&diagnostics=false&callback=top_stories'></script> </body> </html> 

All he does is get the very first news from Yahoo! home page

+2
source

All Articles