<!--小说爬虫-->
<dependency>
<groupId>us.codecraft</groupId>
<artifactId>webmagic-core</artifactId>
<version>0.7.5</version>
</dependency>
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.selector.Html;
import us.codecraft.webmagic.selector.Selectable;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* 小说爬虫
*/
public class fictionClass implements PageProcessor {
//设置带爬取文件的相关配置
private final Site site = Site.me()
.setCharset("utf-8") //设置字符集
.setTimeOut(1000) //设置超时时间
.setSleepTime(1000); //设置休眠时间
@Override
public Site getSite() {
return site;
}
// 小说名
private String title;
//内容集合
List<String> content = new ArrayList<>();
/**
* Spider.create(new FictionDownload()).thread(1).addUrl("").run() 的时候会自动调用一次这个方法
* page.addTargetRequests(List<String> urls) 添加待爬虫队列 数组
* page.addTargetRequest(String requestString) 添加待爬虫队列 单条
*
* @param page 当前的页面
*/
@Override
public void process(Page page) {
//获取当前读取的 URL
Selectable table = page.getUrl();
Html html = page.getHtml();//获取当前页面
if (table.toString().equals("http://www.wxshuku.net/77/77209/")) {
// 如果当前链接是 首页 就会返回 true,进入这里
// 这里我用的章节首页,小说最后一章完成会自动跳转到这里,做一些结尾的事情
//获取书名
title = html.xpath("[@class='book_news_style_text2']/h1/text()").toString();
downBook(content);
} else {
try {
//获取章节名
String chapter = html.xpath("[@class='novel_head']/h1/text()").toString();
//获取文章内容 [@id='content'] id为 content 的全部内容包括标签 /text() 获取文本
List<String> temp = html.xpath("[@class='novel_content']").all();
// 对文章内容提纯处理
String br = " <br> \n" +
" <br> ";
// class="novel_content"
String[] split = temp.get(0).split(br);
split[0] = split[0].substring("<div class=\"novel_content\">\n".length() +
" ".length());
split[0]=split[0].replaceAll("<br>","").replaceAll(" ","").replaceAll("ew\">最新网址:www.wxshuku.net</p> 更*多精;彩'小*说'尽|在'.'''. 第'一;'*小'说*站","")
.replaceAll("\\[","").replaceAll("]","").replaceAll("'\\)","");
split[split.length - 1] = split[split.length - 1].substring(0, split[split.length - 1].length() - 6);
content.add(chapter + "\n\n\n");
content.addAll(Arrays.asList(split));
// 获取下一章链接,并添加到爬虫队列
String url = html.xpath("[@class='novel_bottom']/a[4]/@href").toString();
page.addTargetRequest(url);
System.out.println("已为当前队列添加:" + url + "链接!");
}catch (Exception e){
e.printStackTrace();
}
}
}
/**
* 自定义方法,用来持久化数据
* @param content 文本数据
*/
private void downBook(List<String> content) {
//判断目录存不存在
File file = new File("D:/Fiction");
if (!file.exists()) {
//如果不存在目录,则创建目录
boolean mkdirs = file.mkdirs();
}
PrintWriter pw = null; //使用IO流
try {
//存为txt文件及其路径
FileOutputStream fos = new FileOutputStream("D:/Fiction/" + title + ".txt");
pw = new PrintWriter(fos, true);
for (String string : content) {
pw.println(string); // "\r\n" 换行
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
if (pw != null) {
pw.close();//关流
}
}
}
public static void main(String[] args) {
// Spider.create(new FictionDownload()).thread(1).addUrl("").run(); 这个基本是固定写法
// thread(int threadNum)开启多少个线程执行 addUrl(String... urls) 爬取网页的地址,如果是小说,一般是章节目录,用来初始化,但我用的第一章地址
Spider.create(new fictionClass()).thread(1).addUrl("http://xxx.html").run();
}
}
因篇幅问题不能全部显示,请点此查看更多更全内容
Copyright © 2019- oldu.cn 版权所有 浙ICP备2024123271号-1
违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com
本站由北京市万商天勤律师事务所王兴未律师提供法律服务