java爬蟲入門


通用網絡爬蟲又稱全網爬蟲(Scalable Web Crawler),爬行對象從一些種子 URL 擴充到整個 Web,主要為門戶站點搜索引擎和大型 Web 服務提供商采集數據。

今天我寫的主要是一些皮毛入門

現在來看下我們的pom依賴

 

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.javaxl</groupId>
    <artifactId>T226_jsoup</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>T226_jsoup</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <!-- jdbc驅動包 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.44</version>
        </dependency>

        <!-- 添加Httpclient支持 -->
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.2</version>
        </dependency>

        <!-- 添加jsoup支持 -->
        <dependency>
            <groupId>org.jsoup</groupId>
            <artifactId>jsoup</artifactId>
            <version>1.10.1</version>
        </dependency>


        <!-- 添加日志支持 -->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.16</version>
        </dependency>

        <!-- 添加ehcache支持 -->
        <dependency>
            <groupId>net.sf.ehcache</groupId>
            <artifactId>ehcache</artifactId>
            <version>2.10.3</version>
        </dependency>

        <!-- 添加commons io支持 -->
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.5</version>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.47</version>
        </dependency>
    </dependencies>
</project>

我們現在先來爬取一下單張圖片

package com.javaxl.crawler;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

import org.apache.commons.io.FileUtils;
import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.log4j.Logger;

import com.javaxl.util.DateUtil;
import com.javaxl.util.PropertiesUtil;

public class DownloadImg {
    private static Logger logger = Logger.getLogger(DownloadImg.class);
    private static String URL = "https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=2786741331,312930537&fm=26&gp=0.jpg";
    public static void main(String[] args) {
        logger.info("開始爬取首頁:" + URL);
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpGet httpGet = new HttpGet(URL);
        RequestConfig config = RequestConfig.custom().setConnectTimeout(5000).setSocketTimeout(8000).build();
        httpGet.setConfig(config);
        CloseableHttpResponse response = null;
        try {
            response = httpClient.execute(httpGet);
            if (response == null) {
                logger.info("連接超時!!!");
            } else {
                HttpEntity entity = response.getEntity();
                String imgPath = PropertiesUtil.getValue("blogImages");
                String dateDir = DateUtil.getCurrentDatePath();
                String uuid = UUID.randomUUID().toString();
                String subfix = entity.getContentType().getValue().split("/")[1];
                String localFile = imgPath+dateDir+"/"+uuid+"."+subfix;
//                System.out.println(localFile);
                FileUtils.copyInputStreamToFile(entity.getContent(), new File(localFile));
            }
        } catch (ClientProtocolException e) {
            logger.error(URL+"-ClientProtocolException", e);
        } catch (IOException e) {
            logger.error(URL+"-IOException", e);
        } catch (Exception e) {
            logger.error(URL+"-Exception", e);
        } finally {
            try {
                if (response != null) {
                    response.close();
                }
                if(httpClient != null) {
                    httpClient.close();
                }
            } catch (IOException e) {
                logger.error(URL+"-IOException", e);
            }
        }
        

        logger.info("結束首頁爬取:" + URL);
    
    }
}

在來看下配置文件

 

 再來看下運行結果

 

 簡單的爬單圖片就結束了,下面我們來爬下數據到數據庫(就選擇博客園的爬取把)

BlogCrawlerStarter.java

package com.javaxl.crawler;

import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.apache.commons.io.FileUtils;
import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.Logger;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.javaxl.util.DateUtil;
import com.javaxl.util.DbUtil;
import com.javaxl.util.PropertiesUtil;

import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Status;

/**
 * @author Administrator
 *
 */
public class BlogCrawlerStarter {

    private static Logger logger = Logger.getLogger(BlogCrawlerStarter.class);
//    https://www.csdn.net/nav/newarticles
    private static String HOMEURL = "https://www.cnblogs.com/";
    private static CloseableHttpClient httpClient;
    private static Connection con;
    private static CacheManager cacheManager;
    private static Cache cache;

    /**
     * httpclient解析首頁,獲取首頁內容
     */
    public static void parseHomePage() {
        logger.info("開始爬取首頁:" + HOMEURL);
        
        cacheManager = CacheManager.create(PropertiesUtil.getValue("ehcacheXmlPath"));
        cache = cacheManager.getCache("cnblog");
        
        httpClient = HttpClients.createDefault();
        HttpGet httpGet = new HttpGet(HOMEURL);
        RequestConfig config = RequestConfig.custom().setConnectTimeout(5000).setSocketTimeout(8000).build();
        httpGet.setConfig(config);
        CloseableHttpResponse response = null;
        try {
            response = httpClient.execute(httpGet);
            if (response == null) {
                logger.info(HOMEURL + ":爬取無響應");
                return;
            }

            if (response.getStatusLine().getStatusCode() == 200) {
                HttpEntity entity = response.getEntity();
                String homePageContent = EntityUtils.toString(entity, "utf-8");
                // System.out.println(homePageContent);
                parseHomePageContent(homePageContent);
            }

        } catch (ClientProtocolException e) {
            logger.error(HOMEURL + "-ClientProtocolException", e);
        } catch (IOException e) {
            logger.error(HOMEURL + "-IOException", e);
        } finally {
            try {
                if (response != null) {
                    response.close();
                }

                if (httpClient != null) {
                    httpClient.close();
                }
            } catch (IOException e) {
                logger.error(HOMEURL + "-IOException", e);
            }
        }

        if(cache.getStatus() ==  Status.STATUS_ALIVE) {
            cache.flush();
        }
        cacheManager.shutdown();
        logger.info("結束爬取首頁:" + HOMEURL);

    }

    /**
     * 通過網絡爬蟲框架jsoup,解析網頁類容,獲取想要數據(博客的連接)
     * 
     * @param homePageContent
     */
    private static void parseHomePageContent(String homePageContent) {
        Document doc = Jsoup.parse(homePageContent);
        //#feedlist_id .list_con .title h2 a
        Elements aEles = doc.select("#post_list .post_item .post_item_body h3 a");
        for (Element aEle : aEles) {
//            這個是首頁中的博客列表中的單個鏈接URL
            String blogUrl = aEle.attr("href");
            if (null == blogUrl || "".equals(blogUrl)) {
                logger.info("該博客未內容,不再爬取插入數據庫!");
                continue;
            }
            if(cache.get(blogUrl) != null) {
                logger.info("該數據已經被爬取到數據庫中,數據庫不再收錄!");
                continue;
            }
//            System.out.println("************************"+blogUrl+"****************************");
            
            parseBlogUrl(blogUrl);
        }
    }

    /**
     * 通過博客地址獲取博客的標題,以及博客的類容
     * 
     * @param blogUrl
     */
    private static void parseBlogUrl(String blogUrl) {

        logger.info("開始爬取博客網頁:" + blogUrl);
        httpClient = HttpClients.createDefault();
        HttpGet httpGet = new HttpGet(blogUrl);
        RequestConfig config = RequestConfig.custom().setConnectTimeout(5000).setSocketTimeout(8000).build();
        httpGet.setConfig(config);
        CloseableHttpResponse response = null;
        try {
            response = httpClient.execute(httpGet);
            if (response == null) {
                logger.info(blogUrl + ":爬取無響應");
                return;
            }

            if (response.getStatusLine().getStatusCode() == 200) {
                HttpEntity entity = response.getEntity();
                String blogContent = EntityUtils.toString(entity, "utf-8");
                parseBlogContent(blogContent, blogUrl);
            }

        } catch (ClientProtocolException e) {
            logger.error(blogUrl + "-ClientProtocolException", e);
        } catch (IOException e) {
            logger.error(blogUrl + "-IOException", e);
        } finally {
            try {
                if (response != null) {
                    response.close();
                }
            } catch (IOException e) {
                logger.error(blogUrl + "-IOException", e);
            }
        }

        logger.info("結束爬取博客網頁:" + HOMEURL);

    }

    /**
     * 解析博客類容,獲取博客中標題以及所有內容
     * 
     * @param blogContent
     */
    private static void parseBlogContent(String blogContent, String link) {
        Document doc = Jsoup.parse(blogContent);
        if(!link.contains("ansion2014")) {
            System.out.println(blogContent);
        }
        Elements titleEles = doc
                //#mainBox main .blog-content-box .article-header-box .article-header .article-title-box h1
                .select("#topics .post h1 a");
        System.out.println("123");
        System.out.println(titleEles.toString());
        System.out.println("123");
        if (titleEles.size() == 0) {
            logger.info("博客標題為空,不插入數據庫!");
            return;
        }
        String title = titleEles.get(0).html();

        Elements blogContentEles = doc.select("#cnblogs_post_body ");
        if (blogContentEles.size() == 0) {
            logger.info("博客內容為空,不插入數據庫!");
            return;
        }
        String blogContentBody = blogContentEles.get(0).html();
        
//        Elements imgEles = doc.select("img");
//        List<String> imgUrlList = new LinkedList<String>();
//        if(imgEles.size() > 0) {
//            for (Element imgEle : imgEles) {
//                imgUrlList.add(imgEle.attr("src"));
//            }
//        }
//        
//        if(imgUrlList.size() > 0) {
//            Map<String, String> replaceUrlMap = downloadImgList(imgUrlList);
//            blogContent = replaceContent(blogContent,replaceUrlMap);
//        }

        String sql = "insert into `t_jsoup_article` values(null,?,?,null,now(),0,0,null,?,0,null)";
        try {
            PreparedStatement pst = con.prepareStatement(sql);
            pst.setObject(1, title);
            pst.setObject(2, blogContentBody);
            pst.setObject(3, link);
            if(pst.executeUpdate() == 0) {
                logger.info("爬取博客信息插入數據庫失敗");
            }else {
                cache.put(new net.sf.ehcache.Element(link, link));
                logger.info("爬取博客信息插入數據庫成功");
            }
        } catch (SQLException e) {
            logger.error("數據異常-SQLException:",e);
        }
    }

    /**
     * 將別人博客內容進行加工,將原有圖片地址換成本地的圖片地址
     * @param blogContent
     * @param replaceUrlMap
     * @return
     */
    private static String replaceContent(String blogContent, Map<String, String> replaceUrlMap) {
        for(Map.Entry<String, String> entry: replaceUrlMap.entrySet()) {
            blogContent = blogContent.replace(entry.getKey(), entry.getValue());
        }
        return blogContent;
    }

    /**
     * 別人服務器圖片本地化
     * @param imgUrlList
     * @return
     */
    private static Map<String, String> downloadImgList(List<String> imgUrlList) {
        Map<String, String> replaceMap = new HashMap<String, String>();
        for (String imgUrl : imgUrlList) {
            CloseableHttpClient httpClient = HttpClients.createDefault();
            HttpGet httpGet = new HttpGet(imgUrl);
            RequestConfig config = RequestConfig.custom().setConnectTimeout(5000).setSocketTimeout(8000).build();
            httpGet.setConfig(config);
            CloseableHttpResponse response = null;
            try {
                response = httpClient.execute(httpGet);
                if (response == null) {
                    logger.info(HOMEURL + ":爬取無響應");
                }else {
                    if (response.getStatusLine().getStatusCode() == 200) {
                        HttpEntity entity = response.getEntity();
                        String blogImagesPath = PropertiesUtil.getValue("blogImages");
                        String dateDir = DateUtil.getCurrentDatePath();
                        String uuid = UUID.randomUUID().toString();
                        String subfix = entity.getContentType().getValue().split("/")[1];
                        String fileName = blogImagesPath + dateDir + "/" + uuid + "." + subfix;
                        
                        FileUtils.copyInputStreamToFile(entity.getContent(), new File(fileName));
                        replaceMap.put(imgUrl, fileName);
                    }
                }
            } catch (ClientProtocolException e) {
                logger.error(imgUrl + "-ClientProtocolException", e);
            } catch (IOException e) {
                logger.error(imgUrl + "-IOException", e);
            } catch (Exception e) {
                logger.error(imgUrl + "-Exception", e);
            } finally {
                try {
                    if (response != null) {
                        response.close();
                    }
                } catch (IOException e) {
                    logger.error(imgUrl + "-IOException", e);
                }
            }
        
        }
        return replaceMap;
    }

    public static void start() {
        while(true) {
            DbUtil dbUtil = new DbUtil();
            try {
                con = dbUtil.getCon();
                parseHomePage();
            } catch (Exception e) {
                logger.error("數據庫連接勢失敗!");
            } finally {
                try {
                    if (con != null) {
                        con.close();
                    }
                } catch (SQLException e) {
                    logger.error("數據關閉異常-SQLException:",e);
                }
            }
            try {
                Thread.sleep(1000*60);
            } catch (InterruptedException e) {
                logger.error("主線程休眠異常-InterruptedException:",e);
            }
        }
    }

    public static void main(String[] args) {
        start();
    }
}

 

看下博客園的博客

 

 運行結果

 

 數據庫的數據


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM