为什么第 4 行出现错误:使用 Google Apps 脚本的 XlmService 的元素类型 "n.length"?
Why Error on line 4: Element type "n.length" with Google Apps Script's XlmService?
我正在尝试使用 Google Apps Script 的 XlmService 抓取 HTML table 数据,其中的功能对我来说是新的。但是我得到一个错误,“异常:第 4 行错误:元素类型“n.length”必须后跟属性规范,“>”或“/>”。”这是因为本网站中的某些内容与 XlmService 不兼容吗?感谢您的任何建议!
function test() {
const url = 'https://www.tsa.gov/coronavirus/passenger-throughput';
const res = UrlFetchApp.fetch(url, { muteHttpExceptions: true }).getContentText();
var res1 = XmlService.parse(res);
}
基于 this answer 我已经使用以下代码段从该站点获取了一些信息:
function test() {
const url = 'https://www.tsa.gov/coronavirus/passenger-throughput';
const res = UrlFetchApp.fetch(url, { muteHttpExceptions: true }).getContentText();
const res1 = Cheerio.load(res);
Logger.log(res1('.views-field-field-2021-throughput').text());
}
大概可以这样解析:
var dates = res1('.views-field-field-today-date').text().match(/\d+\/\d+\/\d+/g);
var col1 = res1('.views-field-field-2021-throughput').text().match(/\d+,\d+[,\d+]*/g);
var col2 = res1('.views-field-field-2020-throughput').text().match(/\d+,\d+[,\d+]*/g);
var col3 = res1('.views-field-field-2019-throughput').text().match(/\d+,\d+[,\d+]*/g);
console.log(dates[0], col1[0], col2[0], col3[0]);
console.log(dates[1], col1[1], col2[1], col3[1]);
console.log(dates[2], col1[2], col2[2], col3[2]);
并以这种方式将数据放在 sheet 上:
var table = dates.map((d, i) => [d, col1[i], col2[i], col3[i]]);
var range = SpreadsheetApp.getActiveSheet().getRange(2,1,table.length,table[0].length);
range.setValues(table);
参考:
https://github.com/tani/cheeriogs
更新
这是代码的更新版本,没有那些笨拙的 RegExp:
function test3() {
const url = 'https://www.tsa.gov/coronavirus/passenger-throughput';
const res = UrlFetchApp.fetch(url, { muteHttpExceptions: true }).getContentText();
const $ = Cheerio.load(res);
var dates = $('table').find('td.views-field-field-today-date').toArray().map(x => $(x).text().trim());
var col1 = $('table').find('td.views-field-field-2021-throughput').toArray().map(x => $(x).text());
var col2 = $('table').find('td.views-field-field-2020-throughput').toArray().map(x => $(x).text());
var col3 = $('table').find('td.views-field-field-2019-throughput').toArray().map(x => $(x).text());
var table = dates.map((d, i) => [d, col1[i], col2[i], col3[i]]);
var range = SpreadsheetApp.getActiveSheet().getRange(2,1,table.length,table[0].length);
range.setValues(table);
}
我正在尝试使用 Google Apps Script 的 XlmService 抓取 HTML table 数据,其中的功能对我来说是新的。但是我得到一个错误,“异常:第 4 行错误:元素类型“n.length”必须后跟属性规范,“>”或“/>”。”这是因为本网站中的某些内容与 XlmService 不兼容吗?感谢您的任何建议!
function test() {
const url = 'https://www.tsa.gov/coronavirus/passenger-throughput';
const res = UrlFetchApp.fetch(url, { muteHttpExceptions: true }).getContentText();
var res1 = XmlService.parse(res);
}
基于 this answer 我已经使用以下代码段从该站点获取了一些信息:
function test() {
const url = 'https://www.tsa.gov/coronavirus/passenger-throughput';
const res = UrlFetchApp.fetch(url, { muteHttpExceptions: true }).getContentText();
const res1 = Cheerio.load(res);
Logger.log(res1('.views-field-field-2021-throughput').text());
}
大概可以这样解析:
var dates = res1('.views-field-field-today-date').text().match(/\d+\/\d+\/\d+/g);
var col1 = res1('.views-field-field-2021-throughput').text().match(/\d+,\d+[,\d+]*/g);
var col2 = res1('.views-field-field-2020-throughput').text().match(/\d+,\d+[,\d+]*/g);
var col3 = res1('.views-field-field-2019-throughput').text().match(/\d+,\d+[,\d+]*/g);
console.log(dates[0], col1[0], col2[0], col3[0]);
console.log(dates[1], col1[1], col2[1], col3[1]);
console.log(dates[2], col1[2], col2[2], col3[2]);
并以这种方式将数据放在 sheet 上:
var table = dates.map((d, i) => [d, col1[i], col2[i], col3[i]]);
var range = SpreadsheetApp.getActiveSheet().getRange(2,1,table.length,table[0].length);
range.setValues(table);
参考:
https://github.com/tani/cheeriogs
更新
这是代码的更新版本,没有那些笨拙的 RegExp:
function test3() {
const url = 'https://www.tsa.gov/coronavirus/passenger-throughput';
const res = UrlFetchApp.fetch(url, { muteHttpExceptions: true }).getContentText();
const $ = Cheerio.load(res);
var dates = $('table').find('td.views-field-field-today-date').toArray().map(x => $(x).text().trim());
var col1 = $('table').find('td.views-field-field-2021-throughput').toArray().map(x => $(x).text());
var col2 = $('table').find('td.views-field-field-2020-throughput').toArray().map(x => $(x).text());
var col3 = $('table').find('td.views-field-field-2019-throughput').toArray().map(x => $(x).text());
var table = dates.map((d, i) => [d, col1[i], col2[i], col3[i]]);
var range = SpreadsheetApp.getActiveSheet().getRange(2,1,table.length,table[0].length);
range.setValues(table);
}