简体   繁体   English

将雅虎金融股票价格抓取到谷歌表格

[英]Scrape yahoo finance stock prices to google sheets

So I can get the xpath working for "mainstream" stocks like CVNA with this line:所以我可以用这条线让 xpath 为像 CVNA 这样的“主流”股票工作:

=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.但是当试图指向像 Oslo exchange 这样的特定交易所时,我得到了一个错误。 Would like to scrape stock prices from NEL.OL想从 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.由于该页面是在客户端而不是服务器端在 javascript 中构建的,因此您将无法通过 importxml / importhtml 函数检索数据。 However, the page contains a json which you can retrieve and analyze to retrieve the information you need.但是,该页面包含一个 json,您可以对其进行检索和分析以检索您需要的信息。

  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 https://docs.google.com/spreadsheets/d/1sTA71PhpxI_QdGKXVAtb0Rc3cmvPLgzvXKXXTmiec7k/copy - G 列

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

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