谷歌应用程序脚本IMPORTXML和xpath

时间:2018-06-01 04:58:38

标签: xpath google-sheets-macros

我正试图将当前的股票价格数据从ASX中提取到谷歌表中

我没有使用= googlefinance(" ASX.NEA","价格")因为该决议对便士股票的价值进行了舍入而导致即时延迟价格。

我没有使用= INDEX(googlefinance(" ASX.NEA","价格",今天() - 10,今天()),2,2)历史价格即使分辨率价格准确,这也无法获得当前的价格。

网址:https://www.asx.com.au/asx/share-price-research/company/NEA xpath(xPath Finder):/ html / body / section [3] / article / div 1 / div / div / div [4] / div 1 / div 1 / company-摘要/表/ tbody的/ TR 1 / TD 1 /跨度 equation:= IMPORTXML(url,xpath) 结果:#N / A错误导入的内容为空

我尝试的其他xpath是: xpath:// table / tbody // span xpath://span[@ng-show="share.last_price"] xpath:// span [@ ng-show =" share.last_price"]

当我查看页面来源时,最新的股价是通过javascript加载的。

示例:股价为0.910 CHrome inspect element

1 个答案:

答案 0 :(得分:0)

使用App脚本的替代解决方案(javascript)

<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;
    }
</code>

这比使用HTML的importXML()或ImportHTML()更加高效,上面的url是jsonresult。

对于其他大型证券交易所(可以在json源找到):

EU USA

相关问题