简体   繁体   中英

How can you get financial data from Google Finance (or yahoo finance) into Google Sheets?

I'm struggling to get financial data into google sheets. I read some answers here and tried this: =importhtml("https://www.google.com/finance?q="&B2&"&fstype=ii&ei=viASWOnfNsfEeqqUnMgG","table",3)

Yet it always shows this error:

Imported content does not have query with given index.

I went through the tables from 2-10 but nothing worked. It would be great if someone can help me with this. If you only know how to get data from yahoo finance or some other stock website then it would be nice if you'd also say that. I tried yahoo finance but there it always says:

URL could not be fetched

or something like that.

For google, there is a formula googlefinance. For yahoo, all datas are imported in the code source within a big json you can fetch by this way

  var source = UrlFetchApp.fetch(url).getContentText()
  var jsonString = source.match(/(?<=root.App.main = ).*(?=}}}})/g) + '}}}}'
  var data = JSON.parse(jsonString) 

you can then navigate through data to retrieve the informations you need.

See formula GOOGLEFINANCE in Google Sheets. It's not totally live (about 20 minutes delay) but for many purposes is enough. I use it mostly for currency exchange rates but there far more options:

https://support.google.com/docs/answer/3093281?hl=en

Yahoo Finance and most of live sites are protected against webscraping or use javascript (that makes parts generated by javascript impossible for import by formulasa like IMPORTHML, IMPORTXML, IMPORTDATA).

I also built a Google sheets add-on that allows you to access fundamental and realtime financial data for tens of thousands of global companies, ETFs and Cryptos. You can access fundamental information like revenue or cashflow from specific years. It's still in its early phases but you can check it out in the add-on store, it's called SheetsFinance (sheetsfinance.com)

Feel free to drop me a line and ask any more questions about it

Regarding cryptocurrency coins you can simply use

=IMPORTDATA("https://cryptoprices.cc/BTC/")

Google Finance only supports the very biggest coins (BTC, ETH), not much more.

Other services usually require fragile web page parsing which is difficult, error prone and not a long term solution.

From looking at some of your comments, you may be looking at something more complex and get a lot more data together. That will probably require access to paid services and some software programming to get it working together.

Google Sheets is not a scraping software, it can pull data from websites and do some limited parsing, but most websites protect against it and or may change over time. Trying to do what you want to do may be the best way to do it.

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