oboer oboer - 1 year ago 64
Javascript Question

Jquery - how to make this code work? (extracting info from another website)

I've found nice piece of code that seems to work, but I have difficulites to adapt it to my needs (never used Jquery, just very basics of Java).
I was able to figure out a part of it, but haven't succeed to make it work.

The website I'm trying to extract info from: HERE. I want to access the value of child (h2) present in div class="cash" (the amount of money raised).

I'm trying to adapt this code (jfiddle):

var url='http://query.yahooapis.com/v1/public/yql?q=select * from html where url=\'http://stackoverflow.com/\' and xpath=\'//div[@id="question-mini-list"]//h3//a\'&format=json&callback=?';

$.getJSON( url, function(data){
$.getJSON( url, function(data){
$.each(data.query.results.a, function(){
$('body').append('<div><a href="http://stackoverflow.com'+this.href +'">'+this.content+'</a></div>')

I've changed the url into:

var url='http://query.yahooapis.com/v1/public/yql?q=select * from html where url=\'http://rycerzeiksiezniczki.pl/kids/agatka/?lang=pl' and xpath=\'//div[@class="cash"]//h2\'&format=json&callback=?';

(replaced website URL and xpath).
Played also with the rest of the code (url, and this.stuff), but no success (fiddle).

What should I change to make it work?

Answer Source

I think you escaped the URL string wrong: '..../?lang=pl'... --> '..../?lang=pl\'...

The ' after the lang query parameter should be escaped like \'.

Additional your xpath query is not precise enough: This should work:

var url='http://query.yahooapis.com/v1/public/yql?q=select * from html where url=\'http://rycerzeiksiezniczki.pl/kids/agatka/?lang=pl\' and xpath=\'//div[@class="row cash"]//h2\'&format=json&callback=?';

$.getJSON( url, function(data){
    $.each(data.query.results, function(){  

        $('body').append('<div><a href="http://rycerzeiksiezniczki.pl/kids/agatka/?lang=pl'+'">'+this.content+' ' + this.strong + '</a></div>')          


The tag you want to extract contains the classes row and cash.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download