航班数据的读取和分析java_java爬取航班实时数据 -- 逼格提升之路

importjava.io.IOException;importjava.text.SimpleDateFormat;importjava.util.Date;importjava.util.HashMap;importjava.util.Map;importjava.util.Timer;importjava.util.TimerTask;importorg.apache.commons.lang.StringUtils;importorg.jsoup.Jsoup;importorg.jsoup.nodes.Document;importorg.jsoup.select.Elements;importcom.jdkeji.sdcx.common.exceptions.SdcxException;importcom.jdkeji.sdcx.common.model.Flight;importnet.minidev.json.JSONObject;/*** 携程 航班爬取数据工具类

* @Description:

*@author:haoqingshuang

* @CreatTime:2017年9月28日

**/

public classXcFlightUtil {public static Map cacheMap = new HashMap();public staticTimer cacheFlightTimer;static{

clearCacheMap();

}public static synchronizedTimer getFlightTimer(){if(cacheFlightTimer == null){return cacheFlightTimer = newTimer();

}else{returncacheFlightTimer;

}

}//定时清空航班缓存数据 立即清空 10分钟

public static voidclearCacheMap(){

TimerTask timertask= newjava.util.TimerTask() {

@Overridepublic voidrun() {

cacheMap.clear();

}

};

Timer cacheFlightTimer=getFlightTimer();

cacheFlightTimer.schedule(timertask,0,600000);

}public static Document getDocument(String url) throwsSdcxException{try{returnJsoup.connect(url).get();

}catch(IOException e) {throw new SdcxException("获取航班信息异常,请自行查询航班数据!");

}

}/*** 根据航班号 爬取航班信息

* @description:

*@author:haoqingshuang

* @CreateDate:2017年9月28日*/

public static JSONObject findFlightByFlightCode(String FlightNumber) throwsSdcxException{

Flight flightModel= newFlight();if(StringUtils.isEmpty(FlightNumber)){throw new SdcxException("请输入航班号!");

}for(String key : cacheMap.keySet()) {if(FlightNumber.equals(key)){if(null !=cacheMap.get(key)){returncacheMap.get(key).getFlightJsonobject();

}

}

}

String nowDate= new SimpleDateFormat("yyyyMMdd").format(newDate());

Document doc= getDocument("http://flights.ctrip.com/actualtime/fno--"+FlightNumber.trim()+"-"+nowDate+".html");if(null ==doc){throw new SdcxException("网络异常,请稍后再试!");

}//航班详情

Elements flightDetail = doc.select("[class=detail-m]");

Elements detailfly= flightDetail.select("[class=detail-fly]");

Elements inldepartureTime= detailfly.select("[class=inl departure] [class=time]");

flightModel.setActualFlyTime(commonIsNull(inldepartureTime));

Elements inldeparturegray= detailfly.select("[class=inl departure] [class=gray]");

flightModel.setPlanFlyTime(commonIsNull(inldeparturegray));

Elements inlbetween= detailfly.select("[class=inl between]");

flightModel.setFlightStatus(commonIsNull(inlbetween));

Elements inlarriveTime= detailfly.select("[class=inl arrive] [class=time]");

flightModel.setActualArriveTime(commonIsNull(inlarriveTime));

Elements inlarrivegray= detailfly.select("[class=inl arrive] [class=gray]");

flightModel.setPlayArriveTime(commonIsNull(inlarrivegray));

Elements detailroute= flightDetail.select("[class=detail-fly detail-route]");

Elements routeinldeparture= detailroute.select("[class=inl departure] p");

flightModel.setSetoutAddress(commonIsNull(routeinldeparture));

Elements routeinlbetween= detailroute.select("[class=inl between]").select("p");

flightModel.setFlyData(commonIsNull(routeinlbetween));

Elements routeinlarrive= detailroute.select("[class=inl arrive] p");

flightModel.setArriveAddres(commonIsNull(routeinlarrive));

Elements additionalDetail= doc.select("[class=detail-info] [class=operation]");

Elements operation= additionalDetail.select("[class=item]");

flightModel.setFlyAdditional(commonIsNull(operation));

Elements flightT= doc.select("[class=detail-t]");

Elements flightCompany= flightT.select("[class=ml5]");

flightModel.setFlightCompay(commonIsNull(flightCompany));

Elements flightCode= flightT.select("strong");

flightModel.setFlightCode(commonIsNull(flightCode));

Elements flightPunctuality= doc.select("[class=f14 gray ml10]");

flightModel.setPunctualityRate(commonIsNull(flightPunctuality));

cacheMap.put(flightModel.getFlightCode(), flightModel);returnflightModel.getFlightJsonobject();

}public staticString commonIsNull(Elements elements) {try{if(null !=elements){if(null != elements.get(0)){return elements.get(0).text();

}

}return "";

}catch(Exception e) {return "";

}

}public staticString commonDoubleIsNull(Elements elements) {try{

StringBuilder sBuilder= newStringBuilder();if(null !=elements){for (int i = 0; i < elements.size(); i++) {if(null !=elements.get(i)){

sBuilder.append(elements.get(i).text());

}

}

}returnsBuilder.toString();

}catch(Exception e) {return "";

}

}public static voidmain(String[] args) {try{//System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(new Date()));

System.out.println(findFlightByFlightCode("HU7835"));;//System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(new Date()));

} catch(SdcxException e) {

System.out.println(e.getMessage());

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值