1

新手,但一直在玩这个到我有限的知识让我失望的地步

我正在尝试获取股票编号的公司名称,我调用了 Google Finance URL 并收到了 XML 响应。

我想从 XML 响应中提取公司元素和属性(我希望我的 XML 术语是正确的)

使用 Debug 我可以看到大部分内容,但我不明白为什么没有返回“thecompanyname”。

帮助赞赏下面的代码

function myFunction() {

//Base URL for getting the Stock information
// http://www.google.com/ig/api?stock=....


var single_quote_returntxt = UrlFetchApp.fetch("http://www.google.com/ig/api?stock=0005").getContentText();
//Get a single stock quote for HSBC code 0005

var quoteinxmldoc = Xml.parse(single_quote_returntxt,true);
// Put the receieved xml response into XMLdocument format


var topelement = quoteinxmldoc.getElement().getElement();
//Get the top element and the next am guessing here a little 

var wantedelement = topelement.getElements();
//the element level i want

var thecompanyname =  wantedelement[3].getText();
//The company name as a string  


Browser.msgBox("End");

}
4

2 回答 2

2

您正在错误地访问 XML 文档的元素。使用调试器看一下它的结构:

通缉要素

您的wantedelement数组包含对象,每个对象都有一个data键。因此,要访问公司名称,您只需执行以下操作:

var thecompanyname =  wantedelement[3].data;

结果如下:

调试器屏幕截图

于 2013-03-19T12:49:19.950 回答
0

如果您更喜欢纯 GAS 方法并使用 Logger,这里有一种方法可以查看在哪里......

function myFunction() {
var single_quote_returntxt = UrlFetchApp.fetch("http://www.google.com/ig/api?stock=0005").getContentText();
Logger.log(single_quote_returntxt)
var quoteinxmldoc = Xml.parse(single_quote_returntxt,true);
var topelements = quoteinxmldoc.getElement().getElement().getElements();
for(var e in topelements){
  var element = topelements[e]
  var att = element.getAttributes();
    for(var a in att){Logger.log('Element '+e+' ='+element.getName().getLocalName()+' attribute :'+ att[a].getName().getLocalName()+'  :  '+att[a].getValue())}
  var wantedelement = element.getElements();
  for(var w in wantedelement){Logger.log('wantedelement '+w+' :'+wantedelement[w].getText())}
      }
}

记录器.log =

Element 0 =symbol attribute :data  :  0005
Element 1 =pretty_symbol attribute :data  :  0005
Element 2 =symbol_lookup_url attribute :data  :  /finance?client=ig&q=0005
Element 3 =company attribute :data  :  HSBC Holdings plc (Hong Kong)
Element 4 =exchange attribute :data  :  HKG
Element 5 =exchange_timezone attribute :data  :  
Element 6 =exchange_utc_offset attribute :data  :  
Element 7 =exchange_closing attribute :data  :  
Element 8 =divisor attribute :data  :  2
Element 9 =currency attribute :data  :  HKD
Element 10 =last attribute :data  :  84.55
Element 11 =high attribute :data  :  84.95
Element 12 =low attribute :data  :  84.10
Element 13 =volume attribute :data  :  17368416
Element 14 =avg_volume attribute :data  :  12414
Element 15 =market_cap attribute :data  :  1562574.91
Element 16 =open attribute :data  :  84.40
Element 17 =y_close attribute :data  :  84.15
Element 18 =change attribute :data  :  +0.40
Element 19 =perc_change attribute :data  :  0.48
Element 20 =delay attribute :data  :  15
Element 21 =trade_timestamp attribute :data  :  5 hours ago
Element 22 =trade_date_utc attribute :data  :  20130319
Element 23 =trade_time_utc attribute :data  :  075959
Element 24 =current_date_utc attribute :data  :  20130319
Element 25 =current_time_utc attribute :data  :  132906
Element 26 =symbol_url attribute :data  :  /finance?client=ig&q=0005
Element 27 =chart_url attribute :data  :  /finance/chart?q=HKG:0005&tlf=12
Element 28 =disclaimer_url attribute :data  :  /help/stock_disclaimer.html
Element 29 =ecn_url attribute :data  :  
Element 30 =isld_last attribute :data  :  
Element 31 =isld_trade_date_utc attribute :data  :  
Element 32 =isld_trade_time_utc attribute :data  :  
Element 33 =brut_last attribute :data  :  
Element 34 =brut_trade_date_utc attribute :data  :  
Element 35 =brut_trade_time_utc attribute :data  :  
Element 36 =daylight_savings attribute :data  :  true

但是 Mogsdad 方法更清洁、更快……毫无疑问 ;-)

于 2013-03-19T13:28:56.473 回答