谷歌脚本-从网站论坛解析HTML-并将数据写入工作表

Google script - parse HTML from Website Forum - and Write Data to Sheet

本文关键字:数据 工作 HTML- 脚本 网站 论坛 谷歌      更新时间:2024-07-02

我从论坛url中获取HTML,并从用户的个人资料页面中解析用户的帖子数。我不知道如何将解析后的数字写入谷歌电子表格。

它应该在B列中逐个帐户,直到最后一行,并用计数更新A列。

该脚本没有给我任何错误,但它没有将检索到的值设置到电子表格中。

    function msg(message){
  Browser.msgBox(message);
}
function onOpen() {
  var ui = SpreadsheetApp.getUi();
  ui.createMenu("Update")
    .addItem('Update Table', 'updatePosts')
    .addToUi();
}
function getPostCount(profileUrl){
  var html = UrlFetchApp.fetch(profileUrl).getContentText();
  var sliced = html.slice(0,html.search('Posts Per Day'));  
  sliced = sliced.slice(sliced.search('<dt>Total Posts</dt>'),sliced.length);
  postCount = sliced.slice(sliced.search("<dd> ")+"<dd> ".length,sliced.search("</dd>"));
  return postCount;
}
function updatePosts(){
  if(arguments[0]===false){
    showAlert = false;
  } else {
    showAlert=true;
  }
  var spreadSheet = SpreadsheetApp.getActiveSpreadsheet();
  var accountSheet = spreadSheet.getSheetByName("account-stats");
  var statsLastCol = statsSheet.getLastColumn();
  var accountCount = accountSheet.getLastRow();
  var newValue = 0;
  var oldValue = 0;
  var totalNewPosts = 0;
  for (var i=2; i<=accountCount; i++){ 
    newValue = parseInt(getPostCount(accountSheet.getRange(i, 9).getValue())); 
    oldValue = parseInt(accountSheet.getRange(i, 7).getValue());
    totalNewPosts = totalNewPosts + newValue - oldValue;
    accountSheet.getRange(i, 7).setValue(newValue);    
    statsSheet.getRange(i,statsLastCol).setValue(newValue-todaysValue); 
  }
  if(showAlert==false){
    return 0;
  }
  msg(totalNewPosts+" new post found!");  
}

function valinar(needle, haystack){
  haystack = haystack[0]; 
  for (var i in haystack){
    if(haystack[i]==needle){
      return true;
    } 
  }

  return false;
}

这是我第一次做这样的事情,并从其他网站的例子工作。

我还有一个问题。在函数getPostCount中,我发送函数profileurl。我在哪里申报?

以下是如何从电子表格中获取URL:

function getPostCount(profileUrl){
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var thisSheet = ss.getSheetByName("List1");
  var getNumberOfRows = thisSheet.getLastRow();
  var urlProfile = "";
  var sliced = "";
  var A_Column = "";
  var arrayIndex = 0;
  var rngA2Bx = thisSheet.getRange(2, 2, getNumberOfRows, 1).getValues();
  for (var i = 2; i < getNumberOfRows + 1; i++) { //Start getting urls from row 2
    //Logger.log('count i: ' + i);
    arrayIndex = i-2;
    urlProfile = rngA2Bx[arrayIndex][0];
    //Logger.log('urlProfile: ' + urlProfile);
    var html = UrlFetchApp.fetch(urlProfile).getContentText();
    sliced = html.slice(0,html.search('Posts Per Day'));
    var postCount = sliced.slice(sliced.search("<dd> ")+"<dd> ".length,sliced.search("</dd>"));
    sliced = sliced.slice(sliced.search('<dt>Total Posts</dt>'),sliced.length);
    postCount = sliced.slice(sliced.search("<dd> ")+"<dd> ".length,sliced.search("</dd>"));
    Logger.log('postCount: ' + postCount);
    A_Column = thisSheet.getRange(i, 1);
    A_Column.setValue(postCount);
  };
}

您在一个变量前面缺少var

postCount = sliced.slice(sliced.search("<dd> ")+"<dd> ".length,sliced.search("</dd>"));

那行不通。需要将var放在前面。var postCount = ....

在该功能中:

function updatePosts(){
  if(arguments[0]===false){
    showAlert = false;
  } else {
    showAlert=true;
  }

代码中的任何位置都没有名为arguments的数组。arguments在哪里定义,它是如何将任何值放入其中的?