【发布时间】:2021-11-19 19:43:45
【问题描述】:
受到来自this answer 的 Cheerio 如此便利的启发,我尝试在以下代码中使用它。代码能够通过调用 class="snapshot-td2" 来提取任何表数据,但我只想获取第一个表中的数据。我怎样才能做到这一点? URL 有两个表,分别为 class="snapshot-td2"。它以字符串形式检索它们。我怎样才能把它们放在数组中?感谢您的帮助!
function test() {
const url = 'https://finviz.com/quote.ashx?t=AFRM';
const res = UrlFetchApp.fetch(url, { muteHttpExceptions: true }).getContentText();
const page = Cheerio.load(res);
// The next line returned the tableValue from two tables of having class="snapshot-td2".
// What should be modified to get the tableValue only from the first table?
// The next line returned the tableValue in string. What should be modified to get them in array?
var tableValue = page('.snapshot-td2').text();
console.log(tableValue);
}
【问题讨论】:
标签: google-apps-script web-scraping html-table cheerio