【问题标题】:Trying to get data from this site into google sheets using app script尝试使用应用脚本从该站点获取数据到谷歌表格
【发布时间】:2021-08-02 09:47:17
【问题描述】:

任何人都可以帮助我转换此站点的响应的代码吗 https://services.sia.homeoffice.gov.uk/PublicRegister/

我需要它从谷歌表格中读取许可证号 - 例如: 1012894854698842

并将结果填充到同一个电子表格中

我一直在玩邮递员和网站抓取工具,但仍然无法弄清楚

请帮忙!! 谢谢你

function senddatatoform() {

var url =   'https://services.sia.homeoffice.gov.uk/PublicRegister/SearchPublicRegisterByLicence'; // this is the 'request url' as shown in your browser (not always the url of the form).  

var payload = {
 LicenseNo: "1012894854698842",
 
}//continue with all required post data   

var options = {
method: "POST",
payload: payload
}

var response = UrlFetchApp.fetch(url,options);
/*this is where what you need to do next might vary -- if you're looking for a 'success' page, you might write some code here to verify that the http response is correct based on your needs.
*/ 

const content = response.getContentText();

const regEx = /<div class="ax_h5">\s*(\S+)/g; 
while (match = regEx.exec(content)) {
console.log(match[1]);
}

};
Logger.log

我需要的信息是

名字

牌照号码

角色

许可部门

有效期

状态

状态说明

附加许可条件

许可证编号在每个员工的电子表格中

【问题讨论】:

标签: api google-sheets


【解决方案1】:

function senddatatoform() {
  const colId = 1;
  const colOutput = 2;
  const rowStart = 2;
  const cols = 9;
  
  const sheet = SpreadsheetApp.getActiveSheet();
  const rows = sheet.getLastRow() - rowStart + 1;
  const ids = sheet.getRange(rowStart, colId, rows, 1).getValues();
  const values = new Array(rows);
  for (let i = 0; i < ids.length; i++) {
    const data = getData(ids[i][0]);
    if (!data) { values[i] = new Array(cols); }
    else { values[i] = data; }
  }
  
  sheet.getRange(rowStart, colOutput, rows, cols).setValues(values);
  
  function getData(id) {
    const url = 'https://services.sia.homeoffice.gov.uk/PublicRegister/SearchPublicRegisterByLicence';
    const payload = {
      'LicenseNo': id,
    };
    var options = {
      'method': 'POST',
      'payload': payload
    };
    const response = UrlFetchApp.fetch(url, options);
    const content = response.getContentText();
    const result = content.match(/panel-default">([\s\S]+)<div class="footer/);
    if (!result) { return; }
    const regex = /form-group[\s\S]*?<(?:div|span).*?>([^<>]+)<\/(?:div|span)/g
    const matches = [...result[1].matchAll(regex)];
    if (matches.length === 0) { return; }
    const values = matches.map(match => match[1].trim());
    return values;
  }
}

【讨论】:

  • 谢谢,我需要它提供的所有信息我如何添加更多数据以返回
  • 另外,如何将其添加到电子表格中?谢谢
  • 请调查信息的模式并做match。你可以写appendRow
  • 抱歉,我还是不明白该怎么做,我需要表单返回的所有信息,其中 SIA 编号是要搜索的变量
  • 请编辑您的问题,以清楚地说明预期的输入、输出,以及您要询问代码的具体流程。
【解决方案2】:

可能会改进...

function senddatatoform() {
  var url = 'https://services.sia.homeoffice.gov.uk/PublicRegister/SearchPublicRegisterByLicence'; 
    var payload = {
    LicenseNo: "1012894854698842",
  }
  var options = {
    method: "POST",
    payload: payload
  }

  var response = UrlFetchApp.fetch(url,options);

  Logger.log(response.getContentText().replace(/(\r\n|\n|\r|\t|  )/gm,"").match(/<div class="panel-body">.*<div class="footer">/g)[0].replace(/(<([^>]+)>)/ig,"|").replace(/(\|)+/gm,"\n"))
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多