3

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

Rubén
  • 34,714
  • 9
  • 70
  • 166
Desmond Yjw
  • 43
  • 1
  • 6
  • Does it have to come from Yahoo? Google has a built in =googlefinance() function (https://support.google.com/docs/answer/3093281?hl=en). – mary Mar 12 '21 at 22:56

2 Answers2

2

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.

enter image description here

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:

Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • @Jia Wei Yeap By the way, if to achieve your goal using Google Apps Script is not the direction you expected, I have to apologize. – Tanaike Mar 13 '21 at 05:11
1

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.

enter image description here

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.

Jason E.
  • 1,201
  • 1
  • 3
  • 10
  • I avoid google finance is because ASX stocks has 3 decimals places which Google Finance round it to 2. Need to find the MA for the past 10 days – Desmond Yjw Mar 13 '21 at 01:44
  • You can try manually calculating the price using googlefinance marketcap/shares and round the result to 3 decimal places. `=ROUND(GOOGLEFINANCE("asx:asx", "marketcap")/GOOGLEFINANCE("asx:asx", "shares"), 3)` – Jason E. Mar 15 '21 at 15:10
  • Using that method that you recommend, what if i wish to obtain the stock prices for the past 5 days, how shall i do so ? – Desmond Yjw Mar 17 '21 at 03:14
  • Unfortunately, the method I recommended is not usable if dealing with historical data. – Jason E. Mar 17 '21 at 15:53