Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save bran921007/0a3f1b618665b300518051eba388f0d5 to your computer and use it in GitHub Desktop.
Save bran921007/0a3f1b618665b300518051eba388f0d5 to your computer and use it in GitHub Desktop.
Scrape Google search results -Title, URL by Location
function scrapeGoogle() {
var searchResults=UrlFetchApp.fetch("https://www.google.co.uk/search?q="+encodeURIComponent("keyword finder tool")+"&num=30",{muteHttpExceptions:true});
var titleExp=/<h3 class=\"r\">([\s\S]*?)<\/h3>/gi;
var urlExpression=/<h3 class=\"r\">([\s\S]*?)\&amp\;/gi;
var titleResults=searchResults.getContentText().match(titleExp);
var urlResults=searchResults.getContentText().match(urlExpression);
//To get the actual Title
for(var i in titleResults)
{
var actualTitle=titleResults[i].replace(/(^\s+)|(\s+$)/g, "").replace(/<\/?[^>]+>/gi, "");
Logger.log(actualTitle);
}
//To get the actual URL
for(var i in urlResults)
{
var actualURL=urlResults[i].replace('<h3 class="r"><a href="/url?q=',"").replace('&amp;',"");
Logger.log(actualURL);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment