爬取携程页面酒店信息并且导入到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"));

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