Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I parse an URL's HTML and insert data to a Google Sheet using Google Script?

I'm completely new to Google Apps Script and JavaScript.

I just wanted to:

  1. Read column A and do an URL request.
  2. Parse the URL's HTML to get the title and num count.
  3. Write the title into column B and the num count into column C.

Here's my code:

function getHtml()
{
    var sheet = SpreadsheetApp.getActiveSheet();
    var data = sheet.getDataRange().getValues();
    var url_range = sheet.getRange('A1:A').getValue();
    var response = UrlFetchApp.fetch(url);
    var content = response.getContentText("UTF-8"); 

    var title = new Array();
    var num_count = new Array();

    for (var i = 1; i < data.length; i++) {
        title = content.match(/<title>(.*?)<\/title>/);
        num_count = content.match(/<span class="num_count">(.*?)<\/span>/);
    }

    var ss = SpreadsheetApp.getActiveSpreadsheet();
    var sheet1 = ss.getSheetByName("sheet1");

    for (var i = 1; i < data.length; i++) {
        sheet1.getRange(i,2).setValue(title[i]);
        sheet1.getRange(i,3).setValue(num_count[i]);
    }
}

But I only get the below result:

enter image description here

Please tell me what I should improve.

like image 644
HanYong Avatar asked Feb 24 '26 17:02

HanYong


1 Answers

Assuming that your scraping works I think this is pretty close to what you require.

function getHtml()
{
  var ss=SpreadsheetApp.getActive();
  var sheet=ss.getActiveSheet();
  var range=sheet.getRange(1,1,sheet.getLastRow(),3);                 
  var data=range.getValues();
  for(var i=0;i<data.length;i++){
    var response = UrlFetchApp.fetch(data[i][0]);
    var content = response.getContentText("UTF-8"); 
    data[i][1]=content.match(/<title>(.*?)<\/title>/);
    data[i][2]=content.match(/<span class="num_count">(.*?)<\/span>/);
  }
  rg.setValues(data);
}
like image 75
Cooper Avatar answered Feb 27 '26 06:02

Cooper