首页 技术 正文
技术 2022年11月20日
0 收藏 749 点赞 3,908 浏览 3151 个字

1.概述

极客时间(https://time.geekbang.org/),想必大家都知道的,上面有很多值得大家学习的课程,如下图:

java爬虫系列第四讲-采集"极客时间"专栏文章、视频专辑

本文主要内容

使用webmagic采集极客时间中某个专栏课程生成html
使用webmagic采集视频课程的文件到本地

直接看一下最终效果图

专栏课程生成本地html

java爬虫系列第四讲-采集"极客时间"专栏文章、视频专辑

视频课程中的视频文件采集到本地

java爬虫系列第四讲-采集"极客时间"专栏文章、视频专辑

2.专栏课程视频采集

大家请先买某个课程,然后才可以采集

1.登录极客时间

登录地址: https://time.geekbang.org/

2.极客时间中获取专栏id

java爬虫系列第四讲-采集"极客时间"专栏文章、视频专辑

3.获取cookie

cookie 中存储了当前账号的登录凭证,采集数据的时候需要用到这些信息系,在chrome浏览器中按F12可以获取到cookie信息,如下图:

java爬虫系列第四讲-采集"极客时间"专栏文章、视频专辑

4.获取专栏采集器代码

采集代码比较多,已上传至gitee:https://gitee.com/likun_557/java-pachong

5.将代码导入idea中

java爬虫系列第四讲-采集"极客时间"专栏文章、视频专辑

6.打开代码,设置cookie

修改com.ady01.demo4.jksj.util.CollectorUtil类中**COOKIE_VALUE*的值替换为你的cookie

public static final String COOKIE_VALUE = "_ga=GA1.2.1259366273.1550461508; _gid=GA1.2.556986769.1555908262; GCID=f412bb7-029";
7.设置需要采集的专栏id

修改com.ady01.demo4.jksj.util.CollectorUtilTest中的 cid 的值

@Testpublic void articleList() throws Exception {    //需要采集的专栏id    long cid = 139L;    ColumnDto columnDto = CollectorUtil.articleList(cid);    ColumnCollectorResponse columnCollectorResponse = columnDto.getColumnCollectorResponse();    List<ArticleCollectorResponse> articleCollectorResponseList = columnDto.getArticleCollectorResponseList();    String articleCollectorResponseListJson = FrameUtil.json(articleCollectorResponseList, true);    log.info("articleCollectorResponseList:{}", articleCollectorResponseListJson);    String s = FreemarkerUtil.getFtlToString("column",            FrameUtil.newHashMap(                    "articleCollectorResponseListJson", articleCollectorResponseListJson,                    "columnCollectorResponse", columnCollectorResponse));    //将采集生成的html保存到本地    FileUtils.write(new File("D:\\极客时间\\" + columnCollectorResponse.getColumn_title() + ".html"), s, "utf-8");}
8.运行代码

执行com.ady01.demo4.jksj.util.CollectorUtilTest中的articleList方法,采集成功

java爬虫系列第四讲-采集"极客时间"专栏文章、视频专辑

生成的文件

java爬虫系列第四讲-采集"极客时间"专栏文章、视频专辑

浏览器中打开

java爬虫系列第四讲-采集"极客时间"专栏文章、视频专辑

3.视频专辑采集

1.打开代码,设置cookie

修改com.ady01.demo4.jksjvideo.util.CollectorUtil类中**COOKIE_VALUE*的值替换为你的cookie

public static final String COOKIE_VALUE = "_ga=GA1.2.1259366273.1550461508; _gid=GA1.2.556986769.1555908262; GCID=f412bb7-029";
2.设置需要采集的专栏id

修改com.ady01.demo4.jksjvideo.util.CollectorUtilTest中的 cid 的值

@Test    public void saveCourseDto() throws IOException {        //视频保存的地址        String saveDir = "D:\\极客时间\\%s";        //视频课程id        Long cid = 160L;        CourseDto courseDto = CollectorUtil.courseDto(cid);        log.info("courseDto:{}", FrameUtil.json(courseDto, true));        for (ArticleCollectorResponse articleCollectorResponse : courseDto.getArticleCollectorResponseList()) {            try {                String dir = String.format(saveDir + "\\%s", courseDto.getCourseCollectorResponse().getColumn_title(), articleCollectorResponse.getId());                CollectorUtil.saveFile(articleCollectorResponse, dir);            } catch (IOException e) {                log.error(e.getMessage(), e);            }        }        int i = 1;        for (ArticleCollectorResponse articleCollectorResponse : courseDto.getArticleCollectorResponseList()) {            File file = new File(String.format(saveDir + "\\%s", courseDto.getCourseCollectorResponse().getColumn_title(), articleCollectorResponse.getId()), String.format("%s.%s", articleCollectorResponse.getId(), ".ts"));            String s = FrameUtil.generateCode(i + "", 3, "0", true);            File newFile = new File(String.format(saveDir + "\\video", courseDto.getCourseCollectorResponse().getColumn_title()),                    String.format("%s、%s.%s", s, articleCollectorResponse.getArticle_title().substring(articleCollectorResponse.getArticle_title().indexOf("|") + 2), "ts").replaceAll("\\?", ""));            FileUtils.copyFile(file, newFile);            i++;        }    }
3.运行代码

执行com.ady01.demo4.jksjvideo.util.CollectorUtilTest中的saveCourseDto方法,采集成功

java爬虫系列第四讲-采集"极客时间"专栏文章、视频专辑

4.获取源码

关注公众号:路人甲Java,发送“极客时间”,获取视频采集的源码

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,020
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,513
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,359
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,142
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,772
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,850