繁体   English   中英

Google ImportHTML:在 Yahoo Finance 中找不到资源 URl

[英]Google ImportHTML : Resource URl Not Found with Yahoo Finance

我希望从 Yahoo Finance 获取历史股票价格到 Google Sheet 并收到此错误。 请协助。 如果使用 import xml 会怎样?

https://au.finance.yahoo.com/quote/ASX.AX/history?p=ASX.AX

=IMPORTHTML(D7,"表格",1)

进口HTML

我相信你的目标如下。

  • 您想从https://au.finance.yahoo.com/quote/ASX.AX/history?p=ASX.AX的 URL 中检索表格并将其放入电子表格。

问题和解决方法:

不幸的是,似乎无法使用 URL 中的 IMPORTHTML 和 IMPORTXML 检索该表。 Jason E. 的回答中已经提到了这一点。

但是,幸运的是,当我使用 Google Apps Script 的 UrlFetchApp 测试检索表时,我确认可以检索表。 因此,在这个答案中,作为一种解决方法,我想建议使用 Google Apps 脚本来实现您的目标。 示例脚本如下。

示例脚本:

请将以下示例脚本复制并粘贴到电子表格的脚本编辑器中。 而且,在使用此脚本之前, 请在 Advanced Google services 中启用表格 API 并且,运行myFunction的 function 并请授权范围。 通过此流程,从 URL 中检索表并将其放入活动表中。

function myFunction() {
  const url = "https://au.finance.yahoo.com/quote/ASX.AX/history?p=ASX.AX";
  const res = UrlFetchApp.fetch(url, {muteHttpExceptions: true});
  const tables = res.getContentText().match(/(<table[\w\s\S]+?<\/table>)/g);
  if (!tables || tables.length == 0) throw new Error("No tables. Please confirm URL again.");
  const spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
  const sheet = spreadsheet.getActiveSheet();
  const resource = {requests: [{pasteData: {html: true, data: tables[0], coordinate: {sheetId: sheet.getSheetId()}}}]};
  Sheets.Spreadsheets.batchUpdate(resource, spreadsheet.getId());
}

结果:

运行上述脚本时,将获得以下结果。

在此处输入图像描述

笔记:

  • 此示例脚本适用于https://au.finance.yahoo.com/quote/ASX.AX/history?p=ASX.AX 所以当你更改 URL 时,脚本可能无法使用。 请注意这一点。

参考:

雅虎似乎对其网站进行了一些更改,导致 Google Sheet 的 IMPORT 功能无法正常工作。 这影响了他们的部分(不是全部)网页以及代码。 使用 IMPORTXML 仍然会给您同样的错误。

在此处输入图像描述

我建议使用内置的 GOOGLEFINANCE() function 或找到另一个可以通过 IMPORT 函数抓取的网站,它会为您提供所需的相同数据。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM