Question

I have found sample code but my modifications are not getting any results. The sample code works and is found at Puppy Tweets

This is the way I've modified it and I get no results:

<script>
function handleResponse (json) {
    var results = json.query.results.json.results,
        ul = document.getElementsByTagName( 'ul' )[0],
        li = null;
    for ( var i = 0; i < results.length; i++ ) {
        li = document.createElement( 'li' );
        li.innerHTML = results[i].text;
        ul.appendChild( li );
    }
}
</script>
<script src="select * from html where url="http://www.fishingnotes.com/lakeinfo.php?id=38742" and xpath='//div[@class="lbox"]/div[1]/div/span[1]'"></script>

I've used the YQL Console to verify the Rest Query is correct. What do I need to do?

Était-ce utile?

La solution

Your main mistake is trying to use (only) a YQL query as the source of a JavaScript script. This needs to be a URL, which can be provided for you by the YQL console as highlighted below.

Location of rest query URL in YQL console

So the <script> element might look like:

<script src="http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20html%20where%20url%3D%22http%3A%2F%2Fwww.fishingnotes.com%2Flakeinfo.php%3Fid%3D38742%22%20and%20xpath%3D'%2F%2Fdiv%5B%40class%3D%22lbox%22%5D%2Fdiv%5B1%5D%2Fdiv%2Fspan%5B1%5D'&callback=handleResponse"></script> 

There are also a couple of minor issues with the JavaScript itself. A basic working version might look like:

<script>
function handleResponse (json) {
    var ul = document.getElementsByTagName( 'ul' )[0],
        li = null;
    for ( var i = 0; i < json.query.count; i++ ) {
        li = document.createElement( 'li' );
        li.innerHTML = json.results[i];
        ul.appendChild( li );
    }
}
</script>

See » this example on JS Bin.

Autres conseils

script src="select * from html where url="http://www.fishingnotes.com/lakeinfo.php?id=38742" and xpath='//div[@class="lbox"]/div[1]/div/span[1]'">

Untested, but it looks like there are problems with the nested quotes here, so first change url="http://www.fishingnotes.com/lakeinfo.php?id=38742" to url='http://www.fishingnotes.com/lakeinfo.php?id=38742'. Then there are quotes within quotes within quotes in your xpath, and I'm not sure it's possible to handle that inline.

Try storing "lbox" in a variable and refer to the variable instead of quoting directly.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top