【发布时间】:2026-02-20 03:50:02
【问题描述】:
我正在尝试从https://finance.yahoo.com/quote/CL%3DF/history?p=CL%3DF 获取整个表格数据。在浏览器上,网页默认显示截至 2020 年 10 月 12 日的 1 年数据。但是以下代码由于某种原因没有提取整个表数据。它只提取了部分数据,仅提取了不到 5 个月的数据,直到 2021 年 5 月 20 日。我错过了什么?任何人都可以帮助修复代码中的任何错误吗?谢谢!
function test() {
const url = 'https://finance.yahoo.com/quote/CL%3DF/history?p=CL%3DF';
const res = UrlFetchApp.fetch(url, { muteHttpExceptions: true }).getContentText();
const $ = Cheerio.load(res);
// The URL webpage shows one year data down to Oct 12, 2021 on the browser.
// But the code below got data only down to May 20, 2020. Why am I mssing?
var data = $('table').find('td').toArray().map(x => $(x).text());
console.log(data[data.length-8]); // Print the last row date other than the web note
}
【问题讨论】:
标签: google-apps-script web-scraping html-table cheerio