简体   繁体   中英

Scrape yahoo finance stock prices to google sheets

So I can get the xpath working for "mainstream" stocks like CVNA with this line:

=REGEXEXTRACT(INDEX(IMPORTXML("https://finance.yahoo.com/quote/CVNA?p=CVNA";"//*[@id='quote-header-info']");;3); "\d+.\d+|\d+")+0

But when trying to point to a specific exchange like Oslo exchange I get an error. Would like to scrape stock prices from NEL.OL

Any suggestions?

As the page is built in javascript on the client side and not on the server side, you will not be able to retrieve the data by the importxml / importhtml functions. However, the page contains a json which you can retrieve and analyze to retrieve the information you need.

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

for instance, for marketprice, you can retrieve the informations by

function marketPrice(code) {
  var url='https://finance.yahoo.com/quote/'+code
  var source = UrlFetchApp.fetch(url).getContentText()
  var jsonString = source.match(/(?<=root.App.main = ).*(?=}}}})/g) + '}}}}'
  var data = JSON.parse(jsonString)
  var regularMarketPrice = data.context.dispatcher.stores.StreamDataStore.quoteData[code].regularMarketPrice.raw
  return regularMarketPrice
}

https://docs.google.com/spreadsheets/d/1sTA71PhpxI_QdGKXVAtb0Rc3cmvPLgzvXKXXTmiec7k/copy - column G

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