Last active
February 21, 2024 11:49
-
-
Save padolsey/9017857 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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