我正在尝试使用 Google-apps-script 创建一个自动化流程,以从此类页面中抓取价格数据:
https://www.barchart.com/stocks/quotes/$AVVN/price-history/historical
具有挑战性的部分是,网页上的数据是“延迟加载”的,因此我在其他网页上使用的“传统”扫描方法在这里不起作用。
我考虑过解决这个问题的其他方法 - 但是:
- 条形图不提供以下数据: $AVVN 通过 http: //marketdata.websol.barchart.com/getHistory
- 我不想使用“下载”按钮 - 因为这需要 自动登录。
- ImportXML() 不起作用(它适用于网页上的其他表, 但不是我想要的)。
我在以下帖子中发现了类似的问题 - 该帖子收到了 omegastripes 非常详细且内容丰富的回复: Open webpage, select all, copy into sheet
-但是当我运行我的代码时:
function test(){
var url = 'https://www.barchart.com/proxies/core-api/v1/historical/get?symbol=%24AVVN&fields=tradeTime.format(m%2Fd%2Fy)%2CopenPrice%2ChighPrice%2ClowPrice%2ClastPrice%2CpriceChange%2Cvolume%2CsymbolCode%2CsymbolType&startDate=2019-04-15&endDate=2019-07-15&type=eod&orderBy=tradeTime&orderDir=desc&limit=2000&meta=field.shortName%2Cfield.type%2Cfield.description&raw=1';
var options = {
"muteHttpExceptions": false
};
var response = UrlFetchApp.fetch(url, options);
Logger.log(response);
}
-然后我收到以下错误:
Request failed for https://www.barchart.com/proxies/core-api/v1/historical/get?symbol=%24AVVN&fields=tradeTime.format(m%2Fd%2Fy)%2CopenPrice%2ChighPrice%2ClowPrice%2ClastPrice%2CpriceChange%2Cvolume%2CsymbolCode%2CsymbolType&startDate=2019-04-15&endDate=2019-07-15&type=eod&orderBy=tradeTime&orderDir=desc&limit=2000&meta=field.shortName%2Cfield.type%2Cfield.description&raw=1 returned code 500. Truncated server response: <!doctype html> <html itemscope itemtype="http://schema.org/WebPage" lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="wi... (use muteHttpExceptions option to examine full response) (line 57, file "DS#1")
基本上是“哎呀,出了点问题。我们很抱歉...此页面似乎有问题。”...如果您将地址粘贴到浏览器中。
所以我的问题是:如何从此页面抓取数据或者 Barchart 现在是否已成功阻止此抓取选项?
最佳答案
您无法使用 Google Apps 脚本延迟加载网页,因为它们仅返回网页的 HTML 内容,并且不会等待 JavaScript 加载后再返回内容。
一种可能的解决方案是使用 Google Cloud Functions 和 Puppeteer 来加载页面。 Cloud Function 提供了一个 HTTP API,可以通过 URLFetch 服务直接从 Apps 脚本调用。
关于google-apps-script - 如何使用 Google-apps-script 从延迟加载的网页(通过 API)抓取数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57054074/