極客時間(time.geekbang.org/),想必你們都知道的,上面有不少值得你們學習的課程,以下圖:html
直接看一下最終效果圖java
專欄課程生成本地htmlgit
視頻課程中的視頻文件採集到本地web
你們請先買某個課程,而後才能夠採集chrome
登陸地址: time.geekbang.org/json
cookie 中存儲了當前帳號的登陸憑證,採集數據的時候須要用到這些信息系,在chrome瀏覽器中按F12能夠獲取到cookie信息,以下圖:瀏覽器
採集代碼比較多,已上傳至gitee:gitee.com/likun_557/j…cookie
修改com.ady01.demo4.jksj.util.CollectorUtil類中**COOKIE_VALUE*的值替換爲你的cookieide
public static final String COOKIE_VALUE = "_ga=GA1.2.1259366273.1550461508; _gid=GA1.2.556986769.1555908262; GCID=f412bb7-029";
複製代碼
修改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");
}
複製代碼
執行com.ady01.demo4.jksj.util.CollectorUtilTest中的articleList方法,採集成功
生成的文件
瀏覽器中打開
修改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";
複製代碼
修改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++;
}
}
複製代碼
執行com.ady01.demo4.jksjvideo.util.CollectorUtilTest中的saveCourseDto方法,採集成功
關注公衆號:路人甲Java,發送「極客時間」,獲取視頻採集的源碼