【发布时间】:2021-08-15 18:06:02
【问题描述】:
我正在尝试开发基于 Google 表格的投资组合跟踪表,该表能够检索澳大利亚 (ASX) 和美国市场证券的每日价格。
对于美国市场证券,GoogleFinance 功能运行良好。然而,对于 ASX 而言,GoogleFinance 检索信息的能力有点偶然。
Ruben 曾询问过类似的question,Ian Finlay 提供的解决方案适用于大多数情况,即上市公司,但不适用于 PMGOLD 等交易所交易产品。
Ian Finlay使用apps脚本解析json数据的解决方案是:
<code>
function AsxPrice(asx_stock) {
var url = "https://www.asx.com.au/asx/1/share/" + asx_stock +"/";
var response = UrlFetchApp.fetch(url);
var content = response.getContentText();
Logger.log(content);
var json = JSON.parse(content);
var last_price = json["last_price"];
return last_price;
}
对于像 NAB = asx_stock 这样的“正常”公司,脚本运行良好,但对于 PMGOLD 这样的交易所交易产品,它就不行了。
通过一些基本的搜索实验,原因似乎是脚本中的 url 没有指向所需的信息。
对于 NAB = asx_stock,url 响应是
{"code":"NAB","isin_code":"AU000000NAB4","desc_full":"Ordinary Fully Paid","last_price":23.77,"open_price":24.11,"day_high_price":24.21,"day_low_price":23.74,"change_price":-0.15,"change_in_percent":"-0.627%","volume":1469971,"bid_price":23.75,"offer_price":23.77,"previous_close_price":23.92,"previous_day_percentage_change":"-1.239%","year_high_price":27.49,"last_trade_date":"2021-01-29T00:00:00+1100","year_high_date":"2020-02-20T00:00:00+1100","year_low_price":13.195,"year_low_date":"2020-03-23T00:00:00+1100","year_open_price":34.51,"year_open_date":"2014-02-25T11:00:00+1100","year_change_price":-10.74,"year_change_in_percentage":"-31.121%","pe":29.12,"eps":0.8214,"average_daily_volume":6578117,"annual_dividend_yield":2.51,"market_cap":-1,"number_of_shares":3297132657,"deprecated_market_cap":78636614000,"deprecated_number_of_shares":3297132657,"suspended":false}
但是,对于 PMGOLD = asx_stock,url 响应是:
{"code":"PMGOLD","isin_code":"AU000PMGOLD8","desc_full":"Perth Mint Gold","suspended":false}
进行一些相对“非代码合格人员”类型的研究,看起来交易所上市产品的实际网址应该是:
https://www.asx.com.au/asx/1/share/PMGOLD/prices?interval=daily&count=1
对此的 url 响应是:
{"data":[{"code":"PMGOLD","close_date":"2021-01-28T00:00:00+1100","close_price":24.12,"change_price":0.19,"volume":98132,"day_high_price":24.2,"day_low_price":23.9,"change_in_percent":"0.794%"}]}
当我将此 url 替换为 Ian Finlay 的代码并将 var 重命名为“close_price”而不是“last_price”时,没有检索到任何内容。使用的代码是:
function AskPrice(asx) {
var url = "https://www.asx.com.au/asx/1/share/"+ asx +"/prices?interval=daily&count=1";
var response = UrlFetchApp.fetch(url);
var content = response.getContentText();
Logger.log(content);
var json = JSON.parse(content);
var data = json["data"];
return data;
}
我怀疑这是由于两种不同 url 类型的 url 响应的格式不同。也许嵌套? - 我不确定。
有人可以帮忙指出我犯了什么错误吗?
谢谢
【问题讨论】:
标签: json parsing google-apps-script