Forked from erajanraja24/gist:2c51e76f906c49f341f331920111aca3
Created
December 21, 2018 17:13
-
-
Save bran921007/0a3f1b618665b300518051eba388f0d5 to your computer and use it in GitHub Desktop.
Scrape Google search results -Title, URL by Location
This file contains hidden or 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
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]*?)\&\;/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('&',""); | |
Logger.log(actualURL); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment