您的位置:首页 > 编程语言 > Java开发

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

2019-04-22 14:22 393 查看

1.概述

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

本文主要内容

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

直接看一下最终效果图

专栏课程生成本地html

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

2.专栏课程视频采集

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

1.登录极客时间

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

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

3.获取cookie

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

4.获取专栏采集器代码

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

5.将代码导入idea中

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 的值

@Test
public 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方法,采集成功

生成的文件

浏览器中打开

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方法,采集成功

4.获取源码

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

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐