Skip to content

Instantly share code, notes, and snippets.

@youpy
Created March 21, 2009 20:10
Show Gist options
  • Save youpy/82961 to your computer and use it in GitHub Desktop.
Save youpy/82961 to your computer and use it in GitHub Desktop.
/* appjet:version 0.1 */
print(H1(appjet.appName));
print(P(A({href:'http://docs.lib-html-xpath.appjet.net/'},'documentation')));
/* appjet:library */
/**
* @overview Library for searching html with XPath<br><br>
*
* In particular, it defines the function xget().
*/
import('lib-json');
/**
* This function searches html with XPath.
*
* @param {string} url URL to search
* @param {string} xpath XPath
* @return {array} search result
* @return {string} error message if search failed
*
* @example
var result = xget('http://lib-html-xpath.appjet.net/', '//h1');
print(result.h1); // prints "lib-html-xpath"
*
*/
var xget = function(url, xpath) {
var yql = "select * from html where url=\""
+ url
+ "\" and xpath='"
+ xpath
+ "'";
var url = 'http://query.yahooapis.com/v1/public/yql';
var content = wget(url, {
q: yql,
format: 'json',
callback: ''
});
var json = JSON.parse(content);
if(json.query.diagnostics.warning) {
return json.query.diagnostics.warning[0];
}
if(json.query.diagnostics.url.error) {
return json.query.diagnostics.url['http-status-message'];
}
if(!json.query.results) {
return [];
}
return json.query.results;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment