public
Last active

Scrape content off the World Wildlife Fund's site and pass it back to a jQuery handler

  • Download Gist
gistfile1.htm
HTML
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
<!-- 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>

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.