爬取阮一峰大佬全部的博客,共计16年的

开发环境:

  • Java语言,JKD1.8
  • 开发工具IDEA

抓取过程分析

阮佬的博客就是纯html,没有什么反爬虫限制,我做的就是http请求下载到html页面,然后把里边所有的广告删除了,保留了文章主体。博客是按时间组织的。代码如下,代码注释是爬取思路。总共120行代码,很简单。

talk is cheap show me the code

 private void down() {
        /*
        爬取思路:
        日志网站是以时间节点组织的,以时间为线索爬取所有日志
        最早的日志是2003年12份
        http://www.ruanyifeng.com/blog/2003/12/
        */
        String url = "http://www.ruanyifeng.com/blog/";
        LocalDate now = LocalDate.now();//获取当前日期
        while (now.isAfter(LocalDate.of(2003, 12, 1))) {//网站开始时间 2003年12月1日
            //爬取网站
            int year = now.getYear();//年份
            int month = now.getMonthValue();//月份
            String monthStr = ((month + 100) + "").substring(1, 3);//格式月份 比如1月转为01月
            String href = url + year + "/" + monthStr + "/";
            //下载该月份的所有文章
            String path = "ruanyifeng" + File.separator + year + monthStr;//文档保存目录
            downPage(href, path);
            //时间减一个月
            now = now.minusMonths(1);
        }
    }
    /**
     * 下载一个月份的所有文章
     *
     * @param path 文件存储路径:年份+月份 如 200312
     * @param url  某月份的网址
     */
    private void downPage(String url, String path) {
        if (repeatUrl.contains(url)) {
            return;
        }
        LOG.info("开始下载某月份的所有文章:{}", url);
        /*
         实现思路:
         比如某月份网址:http://www.ruanyifeng.com/blog/2003/12/
         页面分析:由上下两部分组成,上边是该月份最后一篇文章,下边是所有剩下的所有文章
         故爬取分上下两部分,爬取到文章的网址即可
         */
        Document document = SpiderUtil.getDocument(url);
        List<String> urls = new ArrayList<>();
        //得到上部分,最后一篇文章
        Element last = document.selectFirst("div > h2 > a");
        if (last != null) {
            String lastUrl = last.attr("href");
            urls.add(lastUrl);
            //得到下部分,剩下的所有文章
            Elements elements = document.select("#alpha-inner > div > div > ul > li > a");
            elements.forEach(m -> urls.add(m.attr("href")));
            //创建目录
            FileUtil.mkdir(System.getProperty("user.dir") + File.separator + path);
            //下载文章
            urls.forEach(m -> downArticle(m, path));
        }
        //防止重复爬取
        repeatUrl.add(url);
    }

/**
     * 下载文章详情页
     *
     * @param url  文章网址
     * @param path 文件存储路径
     */
    private void downArticle(String url, String path) {
        if (repeatUrl.contains(url)) {
            return;
        }
        LOG.info("开始下载文章详情:{}", url);
        Document document = SpiderUtil.getDocument(url);
        if (document != null) {
            Element title = document.selectFirst("#page-title");
            if(title==null){
                repeatUrl.add(url);
                return;
            }
            Element date = document.selectFirst("article > div > p:nth-child(2) > a > abbr");
            Element content = document.selectFirst("#main-content");
            //文章的图片处理
//            SpiderUtil.saveImages(content, path);
            String html = "<head>\n" +
                    "    <meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" /></head>" +
                    title.toString() + date.toString() + content.toString();
            Document doc = Jsoup.parse(html);
//            String markdown = SpiderUtil.html2markdown(html);//文章转markdown格式
            //保存文档到硬盘
            //得到文件标题并清除特殊符号
            String titleStr = title.text().replaceAll("[\\pP+~$`^=|<>~`$^+=|<>¥×]", "");
            String dateStr = date.text().replaceAll("[年|月|日]", "");
            String filePath = FileUtil.getProjectDir() + path + File.separator + dateStr + "-" + titleStr + ".html";
            File file = FileUtil.newFile(filePath);
            LOG.info("保存文章: {},文章地址: {}", dateStr + titleStr, url);
            FileUtil.writeString(doc.toString(), file, CharsetUtil.UTF_8);
            //防止重复爬取
            repeatUrl.add(url);
        }

    }

项目依赖环境

<?xml version="1.0" encoding="UTF-8"?>
<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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com</groupId>
    <artifactId>zhiyuan</artifactId>
    <version>1.0.0</version>
    <name>zhiyuan</name>
    <description>致远开发团队-项目集合</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <!--使用阿里云maven库,快速下载jar-->
    <repositories>
        <repository>
            <id>central</id>
            <name>aliyun maven</name>
            <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
            <layout>default</layout>
            <!-- 是否开启发布版构件下载 -->
            <releases>
                <enabled>true</enabled>
            </releases>
            <!-- 是否开启快照版构件下载 -->
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>

    <dependencies>
        <!--springboot相关依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--参数校验支持-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-validation</artifactId>
        </dependency>
        <!--aop支持-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>
        <!--测试支持-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!--缓存支持-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-cache</artifactId>
        </dependency>
        <!--jdbc连接数据库支持-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <!--mysql 默认版本-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <!--为支持低版本mysql 引入mysql5.1版本的jar-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.47</version>
        </dependency>

        <!--阿里Druid数据库连接池  https://github.com/alibaba/druid/blob/master/druid-spring-boot-starter/README.md-->
      <!--  <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.10</version>
        </dependency>-->
        <!--Mybatis-plus增强版Mybatis  https://mybatis.plus-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.1.0</version>
        </dependency>
        <!-- Mybatis-plus 代码生成 -->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-generator</artifactId>
            <version>3.1.0</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.velocity/velocity -->
        <dependency>
            <groupId>org.apache.velocity</groupId>
            <artifactId>velocity</artifactId>
            <version>1.7</version>
        </dependency>
        <!--hutool工具包  http://hutool.cn/-->
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>4.4.5</version>
        </dependency>
        <!--图片压缩处理  https://mvnrepository.com/artifact/net.coobird.thumbnailator/thumbnailator -->
        <dependency>
            <groupId>net.coobird</groupId>
            <artifactId>thumbnailator</artifactId>
            <version>0.4.8</version>
        </dependency>
        <!--fastjson-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.15</version>
        </dependency>
        <!--jjwt java版jwt实现   https://github.com/jwtk/jjwt-->
        <dependency>
            <groupId>io.jsonwebtoken</groupId>
            <artifactId>jjwt</artifactId>
            <version>0.9.1</version>
        </dependency>
        <!-- jsoup Java HTML Parser Java版的Html解析工具 https://jsoup.org/-->
        <dependency>
            <groupId>org.jsoup</groupId>
            <artifactId>jsoup</artifactId>
            <version>1.11.3</version>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.3.2</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.13.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.13.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.13.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.13.1</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

image

所有的博客在群文件,可以下载

317896269 点击链接加入群聊【数据爬取技术群】:点击加群

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,736评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,167评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,442评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,902评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,302评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,573评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,847评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,562评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,260评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,531评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,021评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,367评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,016评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,068评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,827评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,610评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,514评论 2 269

推荐阅读更多精彩内容

  • 晚上看之前的《朗读者》。经典节目就是经典,主持人的台词都太优美了。 真的是书到用时方恨少!
    泽蒙阅读 126评论 0 0
  • 冷风吹过泪滴落 细雨洒下冷眼待 ...
    abc奋斗阅读 101评论 0 1
  • 昨天看了电影《北京与西雅图》,2部都看了,颇有感触,简谈一下。 文佳佳怀孕了到美国西雅图来坐月子,她老...
    南方小川阅读 180评论 0 1