个人学习需要,自己也不想造大量的数据(太懒~哈哈~),就爬了一下豆瓣读书的数据(感谢豆瓣~)
流程:使用 Java 的 jsoup 对豆瓣读书进行爬虫,保存到本地 mysql 中,再使用 logstash 将 mysql 的数据传输到 elasticsearch
项目源码:https://github.com/Vmetrio/reptile
jsoup官网:https://jsoup.org
豆瓣读书:https://book.douban.com/latest?icn=index-latestbook-all
一、创建springboot项目,pom文件引入jsoup的依赖
<!-- https://mvnrepository.com/artifact/org.jsoup/jsoup -->
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.12.1</version>
</dependency>
二、分析豆瓣读书的html页面,写对应的爬虫代码
核心代码:
@GetMapping("/reptile")
public List<Books> Index() throws Exception {
//获取url请求
String url = "https://book.douban.com/latest?icn=index-latestbook-all";
//解析网页,Jsoup返回的是Document对象(浏览器Document对象)
Document document = Jsoup.parse(new URL(url), 30000);
//所有在js中使用的方法,这里都能使用
Element element = document.getElementById("content");
//在获得网页内容后,获取所有的li标签
Elements elements = element.getElementsByTag("li");
ArrayList<Books> booksList = new ArrayList<Books>();
//获取元素的标签后,再获取标签中的内容
for (Element el : elements) {
String bookurl = el.getElementsByClass("cover").attr("href");
String imgurl = el.getElementsByTag("img").attr("src");
String bookname = el.getElementsByTag("h2").eq(0).text();
String author = el.getElementsByClass("color-gray").eq(0).text();
String detail = el.getElementsByClass("detail").eq(0).text();
Books books = new Books();
books.setBookurl(bookurl);
books.setImgurl(imgurl);
books.setBookname(bookname);
books.setAuthor(author);
books.setDetail(detail);
booksList.add(books);
}
return booksList;
}