java爬取航班实时数据

使用jsoup获取航班实时数据

优先使用携程航班数据  如果携程航班数据返回为空 则使用去哪儿航班信息

pom.xml 

<dependency>
            <groupId>org.jsoup</groupId>
            <artifactId>jsoup</artifactId>
            <version>1.6.1</version>
        </dependency>
View Code

爬取携程航班信息代码

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.commons.lang.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import com.jdkeji.sdcx.common.exceptions.SdcxException;

/**
 * 携程 航班爬取数据工具类
 * @Description:
 * @author:haoqingshuang
 * @CreatTime:2017年9月28日
 *
 */
public class XcFlightUtil {
        
    //    http://flights.ctrip.com/actualtime/fno--CZ6902-20170928-BJS-URC.html
    public static  Document getDocument(String url) throws SdcxException{                                                         
         try {                                                                                         
             return Jsoup.connect(url).get();                                                          
         } catch (IOException e) {                                                                     
             throw new SdcxException("获取航班信息异常,请自行查询航班数据!");                                                                  
         }                                                                                             
     }
    
    /**
     * 根据航班号 爬取航班信息
     * @description:
     * @author:haoqingshuang
     * @CreateDate:2017年9月28日
     */
    public static String findFlightByFlightCode(String FlightNumber) throws SdcxException{
        StringBuilder sBuffer = new StringBuilder();
        if(StringUtils.isEmpty(FlightNumber)){
            throw new SdcxException("请输入航班号!");
        }
        String nowDate = new SimpleDateFormat("yyyyMMdd").format(new Date());
        Document doc = getDocument("http://flights.ctrip.com/actualtime/fno--"+FlightNumber+"-"+nowDate+".html");  
        if(null == doc){
            throw new SdcxException("网络异常,请稍后再试!");
        }
        
          // 航班详情                                                                          
         Elements flightDetail = doc.select("[class=detail-m]");
         
         //起飞详情
         Elements detailfly = flightDetail.select("[class=detail-fly]"); 
         //起飞地信息
         Elements inldeparture = detailfly.select("[class=inl departure] p");//实际起飞--文字
         sBuffer.append(commonIsNull(inldeparture)==""?"":commonIsNull(inldeparture)+":"); 
         Elements inldepartureTime = detailfly.select("[class=inl departure] [class=time]");//实际起飞--时间
         sBuffer.append(commonIsNull(inldepartureTime)+",");
         Elements inldeparturegray = detailfly.select("[class=inl departure] [class=gray]");
         sBuffer.append(commonIsNull(inldeparturegray)+",");
         //当前飞机状态
         Elements inlbetween = detailfly.select("[class=inl between]");
         sBuffer.append(commonIsNull(inlbetween)==""?"":"当前飞机状态:"+commonIsNull(inlbetween)+",");
         //目的地信息
         Elements inlarrive = detailfly.select("[class=inl arrive] p");
         sBuffer.append(commonIsNull(inlarrive)==""?"":commonIsNull(inlarrive)+":");//预计到达
         Elements inlarriveTime = detailfly.select("[class=inl arrive] [class=time]");
         sBuffer.append(commonIsNull(inlarriveTime)+",");//预计到达时间
         Elements inlarrivegray = detailfly.select("[class=inl arrive] [class=gray]");
         sBuffer.append(commonIsNull(inlarrivegray)+",");//计划到达 文字+时间
         
         //路线详情
         Elements detailroute = flightDetail.select("[class=detail-fly detail-route]");
         Elements routeinldeparture = detailroute.select("[class=inl departure]");
         sBuffer.append(commonIsNull(routeinldeparture)==""?"":"出发地信息:"+commonIsNull(routeinldeparture)+",");//出发地信息 机场、天气情况
         //Elements routegray = routeinldeparture.select("[class=f12] [class=gray ml5]");
         //sBuffer.append(commonIsNull(routegray)==""?"":"出发地天气:"+commonIsNull(routegray)+",");//出发地 天气
         Elements routeinlbetween = detailroute.select("[class=inl between]").select("p");//飞行数据
         sBuffer.append(commonDoubleIsNull(routeinlbetween)==""?"":"飞行数据:"+commonDoubleIsNull(routeinlbetween)+",");
         Elements routeinlarrive = detailroute.select("[class=inl arrive]");//目的地信息 机场、天气情况
         sBuffer.append(commonIsNull(routeinlarrive)==""?"":"目的地信息:"+commonIsNull(routeinlarrive)+",");
         
         //附加信息  值机柜台、登机口、行李转盘
         Elements additionalDetail = doc.select("[class=detail-info] [class=operation]");
         Elements operation = additionalDetail.select("[class=item]");
         sBuffer.append(commonDoubleIsNull(operation)==""?"":"附加信息:"+commonDoubleIsNull(operation));
         
         if(StringUtils.isNotEmpty(sBuffer.toString().replaceAll(",","").replaceAll(" ","").replaceAll("机场攻略","")
                 .replaceAll(":",""))){
             //System.out.println(sBuffer.toString().replaceAll("机场攻略",""));
             return sBuffer.toString().replaceAll("机场攻略","");
         }else{
             return null;
         }
    }
    
    //单个拼接
    public static String commonIsNull(Elements elements) {
        try {
            if(null != elements){
                if(null != elements.get(0)){
                    return elements.get(0).text();
                }
            }
            return "";
        } catch (Exception e) {
            return "";
        }
    }
    
    //多个字符串拼接 适合 多个 class=aaa
    public static String commonDoubleIsNull(Elements elements) {
        try {
            StringBuilder sBuilder = new StringBuilder();
            if(null != elements){
                for (int i = 0; i < elements.size(); i++) {
                    if(null != elements.get(i)){
                        sBuilder.append(elements.get(i).text());
                    }
                }
            }
            return sBuilder.toString();
        } catch (Exception e) {
            return "";
        }
    }
    
    public static void main(String[] args) {
        try {
            //System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(new Date()));
            System.out.println(findFlightByFlightCode("CA1815"));;
            //System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(new Date()));
        } catch (SdcxException e) {
            System.out.println(e.getMessage());
        }
    }
    
    
View Code

爬取去哪儿航班信息代码

import java.io.IOException;
import org.apache.commons.lang.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import com.jdkeji.sdcx.common.exceptions.SdcxException;

/**
 * 去哪儿 爬取航班动态工具类 只能查询今日数据
 * @Description:
 * @author:haoqingshuang
 * @CreatTime:2017年10月10日
 *
 */
public class QnFlightUtil {
    
    public static  Document getDocument(String url) throws SdcxException{                                                         
         try {                                                                                         
             return Jsoup.connect(url).get();                                                          
         } catch (IOException e) {                                                                     
             throw new SdcxException("获取航班信息异常,请自行查询航班数据!");                                                                  
         }                                                                                             
     }
    
    //单个拼接
    public static String commonIsNull(Elements elements) {
        try {
            if(null != elements){
                if(null != elements.get(0)){
                    return elements.get(0).text();
                }
            }
            return "";
        } catch (Exception e) {
            return "";
        }
    }
    
    //多个字符串拼接 适合 多个 class=aaa
    public static String commonDoubleIsNull(Elements elements) {
        try {
            StringBuilder sBuilder = new StringBuilder();
            if(null != elements){
                for (int i = 0; i < elements.size(); i++) {
                    if(null != elements.get(i)){
                        sBuilder.append(elements.get(i).text());
                    }
                }
            }
            return sBuilder.toString();
        } catch (Exception e) {
            return "";
        }
    }
    
    public static String findFlightByFlightCode(String FlightNumber) throws SdcxException{
        StringBuilder sBuffer = new StringBuilder();
        if(StringUtils.isEmpty(FlightNumber)){
            throw new SdcxException("请输入航班号!");
        }
        Document doc = getDocument("http://flight.qunar.com/status/fquery.jsp?flightCode="+FlightNumber.trim());  
        if(null == doc){
            throw new SdcxException("网络异常,请稍后再试!");
        }
        Elements flightDetail = doc.select("[class=state_detail]");
        
        sBuffer.append(commonDoubleIsNull(flightDetail)==""?"":commonDoubleIsNull(flightDetail));
        
        if(StringUtils.isNotEmpty(sBuffer.toString().replaceAll(",","").replaceAll(" ","").replaceAll("相关航班","")
                 .replaceAll(":",""))){
             return sBuffer.toString().replaceAll("相关航班","");
         }else{
             return null;
         }
    }
    
    public static void main(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());
        }
    }
}
View Code

 测试例子

import java.io.IOException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;


public class TestJsoup {
    
    public  Document getDocument (String url){                                                         
         try {                                                                                         
             return Jsoup.connect(url).get();                                                          
         } catch (IOException e) {                                                                     
             e.printStackTrace();                                                                      
         }                                                                                             
         return null;                                                                                  
     }                                                                                                 
                                                                                                       
     public static void main(String[] args) {                                                          
         TestJsoup t = new TestJsoup();                                                                
         Document doc = t.getDocument("http://www.weather.com.cn/html/weather/101280101.shtml");       
         // 获取目标HTML代码                                                                           
         Elements elements1 = doc.select("[class=sky skyid lv2 on]");                                
         // 今天                                                                                       
         Elements elements2 = elements1.select("h1");                                                  
         String today = elements2.get(0).text();                                                       
         System.out.println(today);                                                                    
         // 天气情况                                                                                       
         Elements elements3 = elements1.select("[class=wea]");                                                  
         String number = elements3.get(0).text();                                                      
         System.out.println(number);                                                                   
         // 高的温度                                                                                   
         Elements elements5 = elements1.select("[class=tem]"); 
         Elements elements9 = elements5.select("span");
         String highTemperature = elements9.get(0).text()+"°C";                                       
         System.out.println(highTemperature);                                                          
         // 低的温度   
         Elements elements10 = elements5.select("i");
         String lowTemperature = elements10.get(0).text()+"°C";                                        
         System.out.println(lowTemperature);                                                           
         // 风力                                                                                       
         Elements elements6 = elements1.select("[class=win] i");                                                   
         String wind = elements6.get(0).text();                                                        
         System.out.println(wind);                                                                     
     }                                                                                                 

    
}
View Code

例子网站

http://www.cnblogs.com/xiaoMzjm/p/3899366.html

posted @ 2017-10-10 17:52  不再_单纯  阅读(600)  评论(0)    收藏  举报