简体   繁体   English

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

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

I wish to grab the historical stock price from Yahoo Finance into Google Sheet and received this error.我希望从 Yahoo Finance 获取历史股票价格到 Google Sheet 并收到此错误。 Please assist.请协助。 If using import xml, how will it be?如果使用 import xml 会怎样?

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

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

Import HTML进口HTML

I believe your goal as follows.我相信你的目标如下。

  • You want to retrieve the table from the URL of https://au.finance.yahoo.com/quote/ASX.AX/history?p=ASX.AX and put it to the Spreadsheet.您想从https://au.finance.yahoo.com/quote/ASX.AX/history?p=ASX.AX的 URL 中检索表格并将其放入电子表格。

Issue and workaround:问题和解决方法:

Unfortunately, it seems that the table cannot be retrieved using IMPORTHTML and IMPORTXML from the URL.不幸的是,似乎无法使用 URL 中的 IMPORTHTML 和 IMPORTXML 检索该表。 This has already been mentioned in Jason E.'s answer . Jason E. 的回答中已经提到了这一点。

But, fortunately, when I tested to retrieve the table using UrlFetchApp of Google Apps Script, I confirmed that the table can be retrieved.但是,幸运的是,当我使用 Google Apps Script 的 UrlFetchApp 测试检索表时,我确认可以检索表。 So, in this answer, as a workaround, I would like to propose to achieve your goal using Google Apps Script.因此,在这个答案中,作为一种解决方法,我想建议使用 Google Apps 脚本来实现您的目标。 The sample script is as follows.示例脚本如下。

Sample script:示例脚本:

Please copy and paste the following sample script to the script editor of Spreadsheet.请将以下示例脚本复制并粘贴到电子表格的脚本编辑器中。 And, before you use this script, please enable Sheets API at Advanced Google services .而且,在使用此脚本之前, 请在 Advanced Google services 中启用表格 API And, run the function of myFunction and please authorize the scopes.并且,运行myFunction的 function 并请授权范围。 By this flow, the table is retrieved from the URL and put it to the active sheet.通过此流程,从 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());
}

Result:结果:

When above script is run, the following result is obtained.运行上述脚本时,将获得以下结果。

在此处输入图像描述

Note:笔记:

  • This sample script is for the URL of https://au.finance.yahoo.com/quote/ASX.AX/history?p=ASX.AX .此示例脚本适用于https://au.finance.yahoo.com/quote/ASX.AX/history?p=ASX.AX So when you changed the URL, the script might not be able to be used.所以当你更改 URL 时,脚本可能无法使用。 Please be careful this.请注意这一点。

References:参考:

Yahoo seems to have made some changes to their website resulting for the IMPORT functions of Google Sheet not to work.雅虎似乎对其网站进行了一些更改,导致 Google Sheet 的 IMPORT 功能无法正常工作。 This affected some(not all) of their webpage as well as the tickers.这影响了他们的部分(不是全部)网页以及代码。 Using IMPORTXML will still give you the same error.使用 IMPORTXML 仍然会给您同样的错误。

在此处输入图像描述

I suggest using the built in GOOGLEFINANCE() function or find another website that is scrape-able by IMPORT functions and will give you the same data as you wanted.我建议使用内置的 GOOGLEFINANCE() function 或找到另一个可以通过 IMPORT 函数抓取的网站,它会为您提供所需的相同数据。

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

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