使用jsoup选择此文本

时间:2013-07-11 20:15:20

标签: parsing jsoup

我想知道如何仅选择this page的费率值(目前为0.1501):

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();
    }
}

1 个答案:

答案 0 :(得分:0)

根据这个

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

您需要使用XML解析器,因为您的url返回xml。详见jsoup的作者的答案

我认为您需要通过在运行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 -->

可能值得一提的是免费提供的财务信息被推迟