小说爬虫之JAVA代码的实现(附代码)
2015-04-29 14:38
393 查看
第一次采用Markdown看看效果。
思路:首先找到一篇小说,获取第一章小说的URL,然后根据该URL来获取该章小说的标题、内容和下一章的URL。之后重复类似动作,就能获取到整篇小说的内容了。
实现方法:这里语言采用==Java==,使用了jsoup。jsoup简单的使用方法可以参考这里。
实现过程:首先找到一篇小说,这里以“神墓”为例,我们打开第一章,然后查看网页源代码。
在源码中我们可以看到下一页的url、文章标题和小说内容。我们可以先获取一个Document对象,然后分别根据Element的Id或者样式来获取小说的内容、标题或者下一页URL。
小说中的下一页地址
小说的标题
小说的内容
小说的下一页地址也可以从这里获取
实现代码:新建一个项目spider,导入jsoup-1.7.3.jar包。
新建com.dapeng.bean包,在该包下新建类Article。其主要代码如下:
2.新建com.dapeng.method包,在该包下新建UtilMethod类。主要用于实现获取文章标题、内容、下一章URL等方法。其代码如下:
3.新建com.dapeng.test包,用于测试获取整篇小说。新建GetArticles类。其代码如下:
运行GetArticles方法,可以看到类似如下的效果:
到这里该代码就告一段落了。后续的就不再进行了,比如说将小说插入数据库,然后自己搭建一个小说站点,或者将小说写入到一个txt文档中,不用再看那烦人的广告了。等等。。这里就不再进行了。
最后代码附上。
思路:首先找到一篇小说,获取第一章小说的URL,然后根据该URL来获取该章小说的标题、内容和下一章的URL。之后重复类似动作,就能获取到整篇小说的内容了。
实现方法:这里语言采用==Java==,使用了jsoup。jsoup简单的使用方法可以参考这里。
实现过程:首先找到一篇小说,这里以“神墓”为例,我们打开第一章,然后查看网页源代码。
在源码中我们可以看到下一页的url、文章标题和小说内容。我们可以先获取一个Document对象,然后分别根据Element的Id或者样式来获取小说的内容、标题或者下一页URL。
小说中的下一页地址
小说的标题
小说的内容
小说的下一页地址也可以从这里获取
实现代码:新建一个项目spider,导入jsoup-1.7.3.jar包。
新建com.dapeng.bean包,在该包下新建类Article。其主要代码如下:
package com.dapeng.bean; public class Article { private String id;//id private String title;//标题 private String content;//内容 private String url;//当前章节url private String nextUrl;//下一章url /** *省略getter、setter方法 */ @Override public String toString() { return "Article [id=" + id + ", title=" + title + ", content=" + content + ", url=" + url + ", nextUrl=" + nextUrl + "]"; } }
2.新建com.dapeng.method包,在该包下新建UtilMethod类。主要用于实现获取文章标题、内容、下一章URL等方法。其代码如下:
package com.dapeng.method; import java.io.IOException; import java.util.regex.Matcher; import java.util.regex.Pattern; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import com.dapeng.bean.Article; public class UtilMethod { /** * 根据url获取Document对象 * @param url 小说章节url * @return Document对象 */ public static Document getDocument(String url){ Document doc = null; try { doc = Jsoup.connect(url).timeout(5000).get(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return doc; } /** * 根据获取的Document对象找到章节标题 * @param doc * @return 标题 */ public static String getTitle(Document doc){ return doc.getElementById("title").text(); } /** * 根据获取的Document对象找到小说内容 * @param doc * @return 内容 */ public static String getContent(Document doc){ if(doc.getElementById("content") != null){ return doc.getElementById("content").text(); }else{ return null; } } /** * 根据获取的Document对象找到下一章的Url地址 * @param doc * @return 下一章Url */ public static String getNextUrl(Document doc){ Element ul = doc.select("ul").first(); String regex = "<li><a href=\"(.*?)\">下一页<\\/a><\\/li>"; Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(ul.toString()); Document nextDoc = null; if (matcher.find()) { nextDoc = Jsoup.parse(matcher.group()); Element href = nextDoc.select("a").first(); return "http://www.bxwx.org/b/5/5131/" + href.attr("href"); }else{ return null; } } /** * 根据url获取id * @param url * @return id */ public static String getId(String url){ String urlSpilts[] = url.split("/"); return (urlSpilts[urlSpilts.length - 1]).split("\\.")[0]; } /** * 根据小说的Url获取一个Article对象 * @param url * @return */ public static Article getArticle(String url){ Article article = new Article(); article.setUrl(url); Document doc = getDocument(url); article.setId(getId(url)); article.setTitle(getTitle(doc)); article.setNextUrl(getNextUrl(doc)); article.setContent(getContent(doc)); return article; } }
3.新建com.dapeng.test包,用于测试获取整篇小说。新建GetArticles类。其代码如下:
package com.dapeng.test; import com.dapeng.bean.Article; import com.dapeng.method.UtilMethod; public class GetArticles { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub String firstUrl = "http://www.bxwx.org/b/5/5131/832882.html"; Article article = UtilMethod.getArticle(firstUrl); while(article.getNextUrl() != null && article.getContent() != null && !article.getId().equals("996627")){ article = UtilMethod.getArticle(article.getNextUrl()); System.out.println(article.getId()+"----"+article.getTitle()); } } }
运行GetArticles方法,可以看到类似如下的效果:
832883----第一卷 从神墓中走出 第二章 沧海桑田 832884----第一卷 从神墓中走出 第三章 人世悠悠 832885----第一卷 从神墓中走出 第四章 无解之谜 832886----第一卷 从神墓中走出 第五章 惊艳 832887----第一卷 从神墓中走出 第六章 公主 832888----第一卷 从神墓中走出 第七章 小恶魔 832889----第一卷 从神墓中走出 第八章 烈火仙莲 832890----第一卷 从神墓中走出 第九章 百丈巨蛇 ....
到这里该代码就告一段落了。后续的就不再进行了,比如说将小说插入数据库,然后自己搭建一个小说站点,或者将小说写入到一个txt文档中,不用再看那烦人的广告了。等等。。这里就不再进行了。
最后代码附上。
相关文章推荐
- 小说爬虫之JAVA代码的实现(附代码)
- 用java实现新浪爬虫,代码完整剖析(仅针对当前SinaSignOn有效)
- 基于Java HttpClient和Htmlparser实现网络爬虫代码
- 入门的JAVA爬虫实现(附代码)
- java爬虫学习日记2-宽度优先爬虫代码实现
- 网络爬虫讲解及java代码实现
- 用java实现新浪爬虫,代码完整剖析(仅针对当前SinaSignOn有效)
- 爬虫技术(2)--抓取网页java代码实现
- 简单的java爬虫抓取网页实现代码(未测试)
- 优酷电视剧爬虫代码实现一:下载解析视频网站页面(4)补充正则:java Pattern和Matcher详解
- 优酷电视剧爬虫代码实现一:下载解析视频网站页面(4)补充: Java正则表达式Matcher.group(int group)相关类解析
- 利用C#实现最基本的小说爬虫示例代码
- java实现一个简单的网络爬虫代码示例
- Java简单实现爬虫技术,抓取整个整个网站所有链接+图片+文件(思路+代码)
- 基于Java的网络爬虫实现抓取网络小说(一)
- java实现爬虫爬网站图片的实例代码
- java中驼峰命名和下划线命名互转方法(代码实现)
- java简单实现爬虫、jsoup实现网页抓取、POI实现数据导出Excel
- 【算法】哈希摘要算法,CRC冗余算法,MD摘要算法,纯JAVA基本数据类型代码实现,面向对象
- 【数据结构】串(String、StringBuilder、StringBuffer)的JAVA代码实现