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

小说爬虫之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。其主要代码如下:

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文档中,不用再看那烦人的广告了。等等。。这里就不再进行了。

最后代码附上。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: