Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Get comment count in Google Apps.t using screen scraping. Used in http://mashe.hawksey.info/2011/08/and-the-most-engaging-ouseful-info-post-is/
function getCommentCount10(){
for (var rowIdx=0; rowIdx < sheet.getLastRow(); rowIdx++){
var rowNum = rowIdx+TITLE_ROW+1;
if (sheet.getRange(rowNum, COM_COL).getValue()==10){
var url = sheet.getRange(rowNum,URL_COL).getValue();
//var url = "http://blog.ouseful.info/2008/10/14/data-scraping-wikipedia-with-google-spreadsheets/"
Utilities.sleep(500);
try {
var options =
{
"method" : "get"
};
var response = UrlFetchApp.fetch(url, options);
if (response.getResponseCode() == 200) {
var responseStr = response.getContentText();
var pattern = /<h3 class="reply">(.*?) Responses<\/h3>/;
var matches = responseStr.match(pattern);
var commentCount = matches[1];
sheet.getRange(rowNum, COM_COL).setValue(commentCount);
}
} catch(e) {
Logger.log(e);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment