/**
* Project Name:JavaTest
* File Name:BankOfChinaExchangeRate.java
* Package Name:com.lee.javatest
* Date:2016年7月22日下午1:34:09
* Copyright (c) 2016年7月22日, Pwenlee All Rights Reserved.
*
*/ package com.lee.javatest; import java.io.Serializable;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List; import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements; /**
* ClassName:BankOfChinaExchangeRate <br/>
* Function: 中行外汇牌价. <br/>
* Date: 2016年7月22日 下午1:34:09 <br/>
* @author PwenLee
* @version
* @see
*/
public class BankOfChinaExchangeRate implements Serializable{ private static final Integer DEAFULT_PAGESIZE = 20; private static final long serialVersionUID = -913877619191789389L; /**
* 货币名称 中文简体
*/
private String currency; /**
* 现汇买入价
*/
private BigDecimal buyingRate; /**
* 现钞买入价
*/
private BigDecimal cashBuyingRate; /**
* 现汇卖出价
*/
private BigDecimal sellingRate; /**
* 现钞卖出价
*/
private BigDecimal cashSellingRate; /**
* 外管局中间价
*/
private BigDecimal SAFEMiddleRate; /**
* 中行折算价
*/
private BigDecimal bankConvertRate; /**
* 发布时间
*/
private String dateTime; public String getCurrency() {
return currency;
} public void setCurrency(String currency) {
this.currency = currency;
} public BigDecimal getBuyingRate() {
return buyingRate;
} public void setBuyingRate(BigDecimal buyingRate) {
this.buyingRate = buyingRate;
} public BigDecimal getCashBuyingRate() {
return cashBuyingRate;
} public void setCashBuyingRate(BigDecimal cashBuyingRate) {
this.cashBuyingRate = cashBuyingRate;
} public BigDecimal getSellingRate() {
return sellingRate;
} public void setSellingRate(BigDecimal sellingRate) {
this.sellingRate = sellingRate;
} public BigDecimal getCashSellingRate() {
return cashSellingRate;
} public void setCashSellingRate(BigDecimal cashSellingRate) {
this.cashSellingRate = cashSellingRate;
} public BigDecimal getSAFEMiddleRate() {
return SAFEMiddleRate;
} public void setSAFEMiddleRate(BigDecimal sAFEMiddleRate) {
SAFEMiddleRate = sAFEMiddleRate;
} public BigDecimal getBankConvertRate() {
return bankConvertRate;
} public void setBankConvertRate(BigDecimal bankConvertRate) {
this.bankConvertRate = bankConvertRate;
} public String getDateTime() {
return dateTime;
} public void setDateTime(String dateTime) {
this.dateTime = dateTime;
} /**
*
* BankOfChinaExchangeRate:
* date:日期 例入“2016-07-22”
* time:时间 例如“05:30:00”
* BankOfChinaCurrencyCode 枚举类
* @author PwenLee
* @param startDate
* @param endDate
* @param currencyCode
* @return BankOfChinaExchangeRate
*/
public BankOfChinaExchangeRate (String date, String time, BankOfChinaCurrencyCode currencyCode){
List<String> context = getExchangeRate(date, time, currencyCode);
this.currency = context.get(0);
this.buyingRate = new BigDecimal(context.get(1));
this.cashBuyingRate = new BigDecimal(context.get(2));
this.sellingRate = new BigDecimal(context.get(3));
this.cashSellingRate = new BigDecimal(context.get(4));
this.SAFEMiddleRate = new BigDecimal(context.get(5));
this.bankConvertRate = new BigDecimal(context.get(6));
this.dateTime = context.get(7) + " " + context.get(8);
} /**
* 取当天凌晨05:30:00的数据
*/
public BankOfChinaExchangeRate(){
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
Date date=new Date();
String nowDate=sdf.format(date);
List<String> context = getExchangeRate(nowDate, "05:30:00", BankOfChinaCurrencyCode.USD);
this.currency = context.get(0);
this.buyingRate = new BigDecimal(context.get(1));
this.cashBuyingRate = new BigDecimal(context.get(2));
this.sellingRate = new BigDecimal(context.get(3));
this.cashSellingRate = new BigDecimal(context.get(4));
this.SAFEMiddleRate = new BigDecimal(context.get(5));
this.bankConvertRate = new BigDecimal(context.get(6));
this.dateTime = context.get(7) + " " + context.get(8);
} /**
* 模拟请求url,返回html源码
* @author PwenLee
* @param url
* @return
*/
private static String GetHtml(String url) {
String html = null;
HttpClient httpClient = new DefaultHttpClient();
httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 20000);
HttpGet httpGet = new HttpGet(url);
try {
HttpResponse httpResponse = httpClient.execute(httpGet);
int resStatu = httpResponse.getStatusLine().getStatusCode();
if (resStatu == HttpStatus.SC_OK) {
HttpEntity entity = httpResponse.getEntity();
if (entity != null) {
html = EntityUtils.toString(entity, "utf-8");
}
}
} catch (Exception e) {
//TODO 打成logger
System.out.println("Connect " + url + " error");
e.printStackTrace();
} finally {
httpClient.getConnectionManager().shutdown();
}
return html;
} private List<String> getExchangeRate(String date, String time, BankOfChinaCurrencyCode currencyCode){ Integer totalPage = totalPage(date, time, currencyCode);
List<String> contextList = new ArrayList<String>();
if(totalPage <= 0){
//TODO logger
return contextList;
} String context = "";
for(int i=totalPage;i>=0;i--){
String url = "http://srh.bankofchina.com/search/whpj/search.jsp?erectDate="+date+"&nothing="+date+"&pjname="+currencyCode.getCode()+"&page="+i;
String html = GetHtml(url);
Document doc = Jsoup.parse(html);
Elements linkElements = doc.getElementsByClass("BOC_main");
Elements datas = linkElements.get(0).getElementsByTag("tr");
for (Element ele : datas) {
if(ele.text().indexOf(time) != -1){
context = ele.text();
break;
}
}
if(context != ""){ //TODO 换成StringUtils.isNotBlank
break;
}
} if(context == "") {//TODO 换成StringUtils.isBlank
//TODO logger
return contextList;
}else{
contextList = Arrays.asList(context.split(" "));
}
return contextList;
} public static Integer totalPage(){
Integer totalPage = 0;
try{
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
Date date=new Date();
String nowDate=sdf.format(date);
String url = "http://srh.bankofchina.com/search/whpj/search.jsp?erectDate="+nowDate+"&nothing="+nowDate+"&pjname="+BankOfChinaCurrencyCode.USD.getCode();
String html = GetHtml(url);
//截取网页总条数变量
String stringTemp = html.substring(html.indexOf("m_nRecordCount = "));
//获取变量的值
String totalcount = stringTemp.substring(stringTemp.indexOf("m_nRecordCount = ")+"m_nRecordCount = ".length(),stringTemp.indexOf(";"));
Integer totalnum = Integer.valueOf(totalcount);
if(totalnum % DEAFULT_PAGESIZE == 0){
totalPage = totalnum/DEAFULT_PAGESIZE;
}else{
totalPage = totalnum/DEAFULT_PAGESIZE+1;
}
}catch(Exception e){
//TODO 打成logger
}
return totalPage;
} public static Integer totalPage(String date, String time, BankOfChinaCurrencyCode currencyCode){
Integer totalPage = 0;
try{
String url = "http://srh.bankofchina.com/search/whpj/search.jsp?erectDate="+date+"&nothing="+date+"&pjname="+currencyCode.getCode();
String html = GetHtml(url);
//截取网页总条数变量
String stringTemp = html.substring(html.indexOf("m_nRecordCount = "));
//获取变量的值
String totalcount = stringTemp.substring(stringTemp.indexOf("m_nRecordCount = ")+"m_nRecordCount = ".length(),stringTemp.indexOf(";"));
Integer totalnum = Integer.valueOf(totalcount);
if(totalnum % DEAFULT_PAGESIZE == 0){
totalPage = totalnum/DEAFULT_PAGESIZE;
}else{
totalPage = totalnum/DEAFULT_PAGESIZE+1;
}
}catch(Exception e){
//TODO 打成logger
}
return totalPage;
} @Override
public String toString() {
return "BankOfChinaExchangeRate [currency=" + currency
+ ", buyingRate=" + buyingRate + ", cashBuyingRate="
+ cashBuyingRate + ", sellingRate=" + sellingRate
+ ", cashSellingRate=" + cashSellingRate + ", SAFEMiddleRate="
+ SAFEMiddleRate + ", bankConvertRate=" + bankConvertRate
+ ", dateTime=" + dateTime + "]";
} }

jsoup简单的爬取网页数据的更多相关文章

  1. 使用webdriver+urllib爬取网页数据(模拟登陆,过验证码)

    urilib是python的标准库,当我们使用Python爬取网页数据时,往往用的是urllib模块,通过调用urllib模块的urlopen(url)方法返回网页对象,并使用read()方法获得ur ...

  2. python之爬取网页数据总结(一)

    今天尝试使用python,爬取网页数据.因为python是新安装好的,所以要正常运行爬取数据的代码需要提前安装插件.分别为requests    Beautifulsoup4   lxml  三个插件 ...

  3. python爬虫——爬取网页数据和解析数据

    1.网络爬虫的基本概念 网络爬虫(又称网络蜘蛛,机器人),就是模拟客户端发送网络请求,接收请求响应,一种按照一定的规则,自动地抓取互联网信息的程序.只要浏览器能够做的事情,原则上,爬虫都能够做到. 2 ...

  4. 使用 Python 爬取网页数据

    1. 使用 urllib.request 获取网页 urllib 是 Python 內建的 HTTP 库, 使用 urllib 可以只需要很简单的步骤就能高效采集数据; 配合 Beautiful 等 ...

  5. 使用XPath爬取网页数据

    我们以我的博客为例,来爬取我所有写过的博客的标题. 首先,打开我的博客页面,右键“检查”开始进行网页分析.我们选中博客标题,再次右键“检查”即可找到标题相应的位置,我们继续点击右键,选择Copy,再点 ...

  6. 03:requests与BeautifulSoup结合爬取网页数据应用

    1.1 爬虫相关模块命令回顾 1.requests模块 1. pip install requests 2. response = requests.get('http://www.baidu.com ...

  7. 使用puppeteer爬取网页数据实践小结

    简单介绍Puppeteer Puppeteer是一个Node库,它通过DevTools协议提供高级API来控制Chrome或Chromium.Puppeteer默认以无头方式运行,但可以配置为有头方式 ...

  8. Selenium+Tesseract-OCR智能识别验证码爬取网页数据

    1.项目需求描述 通过订单号获取某系统内订单的详细数据,不需要账号密码的登录验证,但有图片验证码的动态识别,将获取到的数据存到数据库. 2.整体思路 1.通过Selenium技术,无窗口模式打开浏览器 ...

  9. 【推荐】oc解析HTML数据的类库(爬取网页数据)

    TFhpple是一个用于解析html数据的第三方库,本人感觉功能还算可以,只不过在使用前必须配置项目. 配置 1.导入libxml2.tbd 2.设置编译路径 使用 这里使用一个例子来说明 http: ...

随机推荐

  1. iphone 耳机 线控

    有电话呼入时: 按一次接听电话: 快速按两次将电话转到语音信箱: 通话中: 按一次挂断电话: 通话中如果有第二个电话打进来时: 按一次保留当前通话并接听第二个电话: 按住两秒钟不放忽略(拒绝接听)第二 ...

  2. MySql与Oracle的区别总结

    在平时工作中使用这两个数据库的时候要多一些,这两数据库的使用方面存在的一些各自不同的地方,许多面试官也会问这两个的区别.所以,凭着自己的一些经验个感触,来说说这二者的区别. 使用的群众:MySql中小 ...

  3. Excel VBA自定义函数编写(UDF, User-Defined Function)

    虽然知道Microsoft Office Excel可以支持用VB语言来进行复杂的编程和自定义函数的编写,但是一直以来都没有这个需求. 这次遇到的问题是要根据一列数组计算出一个值,但计算过程又比较复杂 ...

  4. 解剖SQLSERVER 第十篇 OrcaMDF Studio 发布+ 特性重温(译)

    解剖SQLSERVER 第十篇  OrcaMDF Studio 发布+ 特性重温(译) http://improve.dk/orcamdf-studio-release-feature-recap/ ...

  5. 在 Cloud 9 中搭建和运行 Go

    简介 自从使用了Chromebook,我脑中一直充斥着在云端开发的念头.在我使用过的位数不多的在线开发环境中,唯有 Cloud 9令我比较满意.实际上,Cloud 9还不支持Go的开发,因此本文我将教 ...

  6. SQL 数据库性能问题排查

    一个项目的运行,总伴随着性能问题,系统查询过慢,如何快速查询等 下面将简单讲解一下,如何去排查及解决这些问题. 开发过程中: 1:不要绝对的三范式,适当建立冗余能够提高查询速度,不用多表关联 2:能用 ...

  7. xtrabackup_binlog_pos_innodb 和 xtrabackup_binlog_info

    用过 xtrabackup 工具的 innobackupex 脚本备份数据的人可能会注意到,–apply-log 处理过的备份数据里有两个文件说明该备份数据对应的 binlog 的文件名和位置.但有时 ...

  8. Backbone源码解析(五):Route和History(路由)模块

    今天是四月十二号,距离上次写博已经将近二十天了.一直忙于工作,回家被看书的时间占用了.连续两个礼拜被频繁的足球篮球以及各种体育运动弄的精疲力竭,所以很少抽时间来写技术博客.今天抽出时间把backbon ...

  9. 团队项目——站立会议 DAY11

    团队项目--站立会议 DAY11        团队成员介绍(5人):张靖颜.何玥.钟灵毓秀.赵莹.王梓萱        今日(2016/5/20),站立会议已进行了两周时间,将这一周所遇到的问题和心 ...

  10. gtest 1.7编译错误:std:tr1:tuple模板参数过多的解决方案

    在gtest/gtest.h文件中添加如下代码 #define _VARIADIC_MAX 10