Let's create a small example. In our company we have created a Google Apps application SignatureSatori to create and setup email signatures for all users in domain. Like a good growth hacker I benchmark the competitors how quickly get new users. There is a Google Apps Marketplace, which estimate number of users. I need save that numbers each day.
1) Create a new Google Apps Script and insert a new library (Resources -> Library)
M1lugvAXKKtUxn_vdAG9JZleS6DrsjUUV
Parser library takes three parameters - input text and pattern which bounds desired text.
Parser // name of library
.data(content) // input text .from(fromText) // from text pattern .to(toText) // to text pattern .build(); // run parser and return value
3) Find the right part of HTML and copy fromText and toText
4) Now we have all required information to complete script
function getData() { var url = "https://chrome.google.com/webstore/detail/signaturesatori-central-s/fejomcfhljndadjlojamaklegghjnjfn?hl=en"; var fromText = '<span class="e-f-ih" title="'; var toText = '">'; var content = UrlFetchApp.fetch(url).getContentText(); var scraped = Parser .data(content) .from(fromText) .to(toText) .build(); Logger.log(scraped); return scraped; }
5) The last and the easiest step is copy parsed data into Spreadsheet
function SAVE_DATA() { var sheet = SpreadsheetApp.openById(spreadsheetId).getSheetByName(sheetName); // insert Spreadsheet Id and Sheet name sheet.appendRow([ new Date(), getData() ]); }
6) If you want to log during scraping (e.g. if you want to debug wrong value), call .setLog() function before final .build() function:
Parser .data(content) .setLog() .from(fromText) .to(toText) .build();Completed code of Parser library Enjoy!