简体   繁体   中英

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. Please assist. If using import xml, how will it be?

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

=IMPORTHTML(D7,"table",1)

Import 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.

Issue and workaround:

Unfortunately, it seems that the table cannot be retrieved using IMPORTHTML and IMPORTXML from the URL. This has already been mentioned in Jason E.'s answer .

But, fortunately, when I tested to retrieve the table using UrlFetchApp of Google Apps Script, I confirmed that the table can be retrieved. So, in this answer, as a workaround, I would like to propose to achieve your goal using Google Apps Script. 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 . And, run the function of myFunction and please authorize the scopes. By this flow, the table is retrieved from the URL and put it to the active sheet.

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 . So when you changed the URL, the script might not be able to be used. 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. This affected some(not all) of their webpage as well as the tickers. Using IMPORTXML will still give you the same error.

在此处输入图像描述

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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