Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@ydn
Created March 3, 2010 00:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ydn/320161 to your computer and use it in GitHub Desktop.
Save ydn/320161 to your computer and use it in GitHub Desktop.
Scrape content off the World Wildlife Fund's site and pass it back to a jQuery handler
<!-- Scrape content off the World Wildlife Fund's site and pass it back to a jQuery handler -->
<!-- Introduced in YDN blog post: http://developer.yahoo.net/blog/archives/2010/03/yql_code_samples_yql_is_easy_to_use.html -->
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script>
function cbfunc(json){
$.each( json.query.results.h4, function ( i, h4 ) {
var div = $( '<div/>' ).text( h4.content );
$.each( json.query.results.ul[i].li, function ( j, li ) {
var ul = $( '<ul/>' ).append( li.p.a.content );
$.each( li.div.a, function ( k, a ) {
var img = $( '<img/>' ).attr( 'src', a.href );
var a = $( '<a/>' ).attr( 'href', a.href ).append( a.title );
var li = $( '<li/>' );
li.append( img );
li.append( '<br/>' );
li.append( a );
ul.append( li );
} );
div.append( ul );
} );
$( document.body ).append( div );
} );
}
$(document).ready(function() {
// run this in the YQL console: http://developer.yahoo.com/yql/console/?q=select%20*%20from%20html%20where%20url%3D%22http%3A%2F%2Fwww.worldwildlife.org%2Fsites%2Fphotos%2Fgalleries.html%22%20and%0A%20%20%20%20%20%20xpath%3D%27%2F%2Fdiv[%40class%3D%22narrativeText%22]%2Fh4|%2F%2Ful[%40id%3D%22speciesList%22]%27
// use ajax() instead of getJSON() as recommended by the yql caching blog post: http://www.yqlblog.net/blog/2010/03/12/avoiding-rate-limits-and-getting-banned-in-yql-and-pipes-caching-is-your-friend/
$.ajax({
url: "http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20html%20where%20url%3D%22http%3A%2F%2Fwww.worldwildlife.org%2Fsites%2Fphotos%2Fgalleries.html%22%20and%0A%20%20%20%20%20%20xpath%3D'%2F%2Fdiv%5B%40class%3D%22narrativeText%22%5D%2Fh4%7C%2F%2Ful%5B%40id%3D%22speciesList%22%5D'&format=json",
dataType: 'jsonp',
jsonp: 'callback',
jsonpCallback: 'cbfunc'
});
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment