爬取攜程頁面酒店信息並且導入到HDFS

進入獲取酒店的頁面
獲取URL:
String url= “http://hotels.ctrip.com/domestic/showhotellist.aspx?utm_medium=&utm_campaign=&utm_source=&isctrip=&allianceid=13963&sid=457771&ouid=000401app-&txtcity=%c9%cf%ba%a3&city=2&starttime=2017-12-06&deptime=2017-12-08&begprice=&endprice=&rooms=&hotelname=&star=&keyword=&locationid=&zoneid= “;

這裏寫圖片描述

package com.itstar.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.itstar.hadoop.HdfsUtil;

/**
 * 大數據文件批量採集下載的工具類
 * @author arry 
 * @version v1.0
 * 
 */
public class DataDownUtil {

    /**
     *  根據頁面的網址和網頁的編碼集來獲取網頁的源代碼
     *  @author arry
     *  @param url 網址
     *  @param encoding 網頁的編碼集
     *  @return String 網頁的源代碼
     *  <br /><br />
     *  <a href="http://baidu.com" style="font-size:30px;color:red;">百度一下,你就知道 !</a>
     *  
     */
    public static String getHtmlResourceByURL(String url,String encoding){

        // 存儲源代碼 容器
        StringBuffer buffer = new StringBuffer();
        URL urlObj = null;
        URLConnection uc = null;
        InputStreamReader isr = null;
        BufferedReader reader = null;

        try {
            // 建立網絡鏈接
            urlObj = new URL(url);
            // 打開網絡連接
            uc = urlObj.openConnection();
            // 建立文件輸入流
            isr = new InputStreamReader(uc.getInputStream(),encoding);
            // 建立文件緩衝寫入流
            reader = new BufferedReader(isr);

            // 建立臨時變量
            String temp = null;
            while((temp = reader.readLine()) != null){
                buffer.append(temp+"\n"); // 一邊讀,一邊寫
            }

        } catch (MalformedURLException e) {
            e.printStackTrace();
            System.out.println("網絡不給力,請檢查設置。");
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("您的網絡鏈接失敗,親稍後重試 !");
        } finally{
            if(isr != null){
                try {
                    isr.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        return buffer.toString();

    }

    /**
     * 解析源代碼,獲取酒店信息
     * @author arry
     * @return List 集合
     * 
     */
    public static List<HashMap<String,Object>> getImgUrl(String html){
        List<HashMap<String,Object>> list = new ArrayList<HashMap<String,Object>>();
        // 解析源代碼,找到需要下載的內容
        Document document = Jsoup.parse(html);
        // 獲取最外層 div  id="hotel_list"
        Element element = document.getElementById("hotel_list");
        // 獲取酒店列表信息
        Elements elements = document.getElementsByClass("hotel_new_list");

        for(Element ele : elements){
            HashMap<String,Object> map = new HashMap<String,Object>();
            // 獲取酒店圖片
            String imgSrc = ele.getElementsByTag("img").attr("src");
            // 酒店名稱
            String title = ele.getElementsByTag("img").attr("alt");
            // 酒店描述信息
            String desc = ele.getElementsByClass("hotel_item_htladdress").text();

            System.out.println("圖片:"+imgSrc);
            System.out.println("酒店名稱:"+title);
            System.out.println("酒店描述信息:"+desc);

            map.put("imgSrc", imgSrc);
            map.put("title", title);
            map.put("desc",desc);

            list.add(map);

            // 下載圖像
            getImg("http:"+imgSrc, "D:\\Windows 7 Documents\\Desktop\\Python\\project\\pic\\");


        }       

        return list;

    }


    /**
     * 下載網絡圖片 
     * @author arry
     * @param imgUrl 網絡圖片的地址
     * @param filePath 服務器存儲圖像的地址
     * @return void 無
     * 
     */
    public static void getImg(String imgUrl,String filePath){

        String fileName = imgUrl.substring(imgUrl.lastIndexOf("/"));

        try{
            // 創建一個服務器文件目錄
            File files = new File(filePath);
            if(!files.exists()){
                files.mkdirs();
            }

            // 獲取下載圖像的網絡鏈接的地址
            URL urlObj = new URL(imgUrl);
            // 打開網絡連接
            HttpURLConnection connetion = (HttpURLConnection)urlObj.openConnection();
            // 獲取文件輸入流
            InputStream is = connetion.getInputStream();
            // 創建文件
            File file = new File(filePath+fileName);
            // 建立文件輸出流
            FileOutputStream fos = new FileOutputStream(file);

            int temp = 0;
            while((temp = is.read()) != -1){
                fos.write(temp);
            }

            is.close();
            fos.close();

        } catch(Exception e){
            e.printStackTrace();
        }
    }



    // Java入口
    public static void main(String[] arsfddfgs){

        System.out.println("親愛的同學們,大家晚上好,我愛你們  !");

        String url = "http://hotels.ctrip.com/domestic/showhotellist.aspx?utm_medium=&utm_campaign=&utm_source=&isctrip=&allianceid=13963&sid=457771&ouid=000401app-&txtcity=%c9%cf%ba%a3&city=2&starttime=2017-12-06&deptime=2017-12-08&begprice=&endprice=&rooms=&hotelname=&star=&keyword=&locationid=&zoneid=";
        String encoding = "utf-8";
        // 1. 根據頁面的網址和網頁的編碼集來獲取網頁的源代碼
        String html = getHtmlResourceByURL(url, encoding);
        //System.out.println(html);

        // 3. 下載圖像和內容信息
        List<HashMap<String,Object>> list = getImgUrl(html);

        System.out.println(list);


        // 4. 同步存儲在大數據hadoop中的HDFS分佈式文件系統中

    }


}

獲取的詳細信息
這裏寫圖片描述

導入到HDFS中

public class HdfsUtil{
    private static FileSystem fs = null;
    static{
        try{
            //配置文件聲明
            Configuration conf = new Configuration();
            //配置文件
            conf.set("fs.defaultFS","localhost");
            //通過API讀取數據
            fs=FileSystem.get(new URI("hdfs://localhost"),conf,"hdfs");
        }catch(Exception e){
            e.printStackTrace();
        }

    }

    /**
     * HDFS 快速文件上傳 
     * @author arry
     * @throws IOException 
     * @throws IllegalArgumentException 
     * 
     */
    @Test
    public void fileUpload() throws IllegalArgumentException, IOException{

        fs.copyFromLocalFile(new Path("D:\\Windows 7 Documents\\Desktop\\Python\\project\\images\\"), new Path("/arry2018"));

    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章