2013-07-11 63 views
0

我不知道选择这段文字我怎么会只选择this page率值(目前0.1501):使用jsoup

public class Currency 
{ 
    public void test() throws IOException 
    { 
     Document doc = Jsoup.connect("http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.xchange%20where%20pair%20in%20(%22SEKUSD%22)&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys").get(); 

     Elements value = doc.select("rate"); 
     System.out.print(value); 
    } 

    public static void main(String args[]) throws IOException 
    { 
     Currency derp = new Currency(); 
     derp.test(); 
    } 
} 

回答

0

根据这一

Use jsoup to parse XML - prevent jsoup from "cleaning" <link> tags

你需要使用XML解析器,因为您的url会返回xml。正如作者在回答中详细说明的那样,我想你需要通过在运行java的计算机上使用curl获取链接来检查是否可以访问url。例如

curl "http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.xchange%20where%20pair%20in%20(%22SEKUSD%22)&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys" -O 

<?xml version="1.0" encoding="UTF-8"?> 
<query xmlns:yahoo="http://www.yahooapis.com/v1/base.rng" 
     yahoo:count="1"yahoo:created="2013-07-11T20:26:10Z" yahoo:lang="en-US"> 
    <results><rate id="SEKUSD"> 
     <Name>SEK to USD</Name><Rate>0.1502</Rate> 
     <Date>7/11/2013</Date><Time>4:23pm</Time> 
     <Ask>0.1503</Ask><Bid>0.1502</Bid> 
    </rate></results> 
</query> 
<\!-- total: 413 --> 
<\!-- engine2.yql.sg3.yahoo.com --> 

它大概值得一提的是,免费提供的金融信息延迟