Skip to content

Instantly share code, notes, and snippets.

@padolsey
Last active February 21, 2024 11:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save padolsey/9017857 to your computer and use it in GitHub Desktop.
Save padolsey/9017857 to your computer and use it in GitHub Desktop.
// Scrape/Grab page using YQL via JSONP
function grab(url, cb) {
var fn = '__grabber' + +new Date;
var query = 'select * from html where url="{URL}" and xpath="*"';
var yurl = 'http' + (/^https/.test(location.protocol)?'s':'') + '://query.yahooapis.com/v1/public/yql?';
yurl += [
'callback=' + fn,
'format=xml',
'q=' + encodeURIComponent(query.replace('{URL}', url))
].join('&');
window[fn] = function(d) {
delete window[fn];
document.body.removeChild(script);
var div = document.createElement('div');
div.innerHTML = d.results[0];
cb(div);
};
var script = document.body.appendChild(document.createElement('script'));
script.src = yurl;
}
grab('http://yahoo.com', function(div) {
console.log(div.querySelector('title').textContent);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment