码迷,mamicode.com
首页 > 其他好文 > 详细

抓取中国银行汇率

时间:2015-01-27 10:45:11      阅读:827      评论:0      收藏:0      [点我收藏+]

标签:

1.使用htmlparser.jericho方法来实现 

2.通过列和行来定位表格元素。获取表格元素 

3.可以获取所有的表格。

4.使用的jar包:jericho-html-3.1

 1 import java.net.URL;
 2 import net.htmlparser.jericho.Element;
 3 import net.htmlparser.jericho.Source;
 4 protected void getRate() {
 5         double USD_CNY_RATE = 0;  //从中国银行获取USD→CNY的汇率
 6         double CNY_USD_RATE = 0;  //转换为CNY→USD的汇率
 7         int index = 0;
 8         Source source = null;
 9         try {
10             source = new Source(new URL(
11                     "http://www.boc.cn/sourcedb/whpj/enindex.html"));
12         } catch (IOException e) {
13             e.printStackTrace();
14         }
15         List<Element> elementList = source.getAllElements("table").get(4)
16                 .getAllElements("tr").get(3).getAllElements("td");
17 
18         for (Element element : elementList) {
19             index++;
20             if (element.getContent().toString().equals("USD")) {
21                 USD_CNY_RATE = Double.parseDouble(elementList.get(index + 4)
22                         .getContent().toString().trim());
23             }
24         }
25         
26         CNY_USD_RATE=Math.round(((USD_CNY_RATE/100))*10000)/10000.0000; //保留小数点后4位
27         bacCurrencyRateService.updateRate(CNY_USD_RATE,"USD","CNY");
28 
29     }

 

抓取中国银行汇率

标签:

原文地址:http://www.cnblogs.com/chen798314508/p/4251969.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!