Jsoup简单例子:抓取笔趣阁小说
直接上代码
public class CrawlText {
/***
* 获取文本
*
* @param autoDownloadFile
* 自动下载文件
* @param Multithreading
* 多线程 默认false
* @param Url
* 网站链接
* @throws IOException
*/
public static void getText(boolean autoDownloadFile, boolean Multithreading, String Url) throws IOException {
String rule = "abs:href";
List<String> urlList = new ArrayList<String>();
Document document = Jsoup.connect(Url)
.timeout(4000)
.ignoreContentType(true)
.userAgent("Mozilla\" to \"Mozilla/5.0 (Windows NT 10.0; WOW64; rv:50.0)")
.get();
System.out.println(document.toString());
Elements urlNode = document.select("a[href$=.html]");
for (Element element : urlNode) {
urlList.add(element.attr(rule));
}
CrawTextThread crawTextThread = new CrawTextThread(urlList);
crawTextThread.start();
}
}
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.List;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
public class CrawTextThread extends Thread {
List<String> UrlList;
public CrawTextThread(List<String> urlList) {
this.UrlList = urlList;
}
String rule = "";
String rule_title = "h1";
String rule_content = "content";
public static String PATH = "D:\\JSOUP\\";
/**
* 创建文件
*
* @param fileName
* @return
*/
public static void createFile(File fileName) throws Exception {
try {
if (!fileName.exists()) {
fileName.createNewFile();
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void writeTxtFile(String content, File fileName) throws Exception {
RandomAccessFile mm = null;
FileOutputStream o = null;
try {
o = new FileOutputStream(fileName);
o.write(content.getBytes("UTF-8"));
o.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (mm != null) {
mm.close();
}
}
}
@Override
public void run() {
currentThread().setName("全部抓取:");
String title;
String content;
for (String url : UrlList) {
try {
Document document = Jsoup.connect(url).timeout(6000).get();
title = document.select("h1").toString();
content = document.select("#content").html();
System.out.println("线程:"+currentThread().getName()+"爬取URL—>"+url);
File file = new File(PATH+title.replaceAll("<h1>", "").replaceAll("</h1>", "")+".txt");
createFile(file);
System.out.println("创建文件:"+file.getPath());
writeTxtFile(FileterHtml(content), file);
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static String FileterHtml(String str) {
return str.replaceAll(" ", "").replaceAll("<br>", "\r\n");
}
}
测试类
public static void main( String[] args )
{
try {
CrawlText.getText(true, true, "http://www.biquge.com.tw/0_66/");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
结果:
创建文件:D:\JSOUP\ 关于新书的种种,在此一并交代.txt
线程:全部抓取:爬取URL—>http://www.biquge.com.tw/0_66/83312.html
创建文件:D:\JSOUP\ 第一章 失意相公.txt
线程:全部抓取:爬取URL—>http://www.biquge.com.tw/0_66/83313.html
创建文件:D:\JSOUP\ 第二章 吊颈秀才.txt
线程:全部抓取:爬取URL—>http://www.biquge.com.tw/0_66/83314.html
创建文件:D:\JSOUP\ 第三章 秦氏族叔.txt
线程:全部抓取:爬取URL—>http://www.biquge.com.tw/0_66/83315.html
创建文件:D:\JSOUP\ 第四章 无妄之灾.txt
线程:全部抓取:爬取URL—>http://www.biquge.com.tw/0_66/83316.html
创建文件:D:\JSOUP\ 第五章 游衙惊梦.txt
线程:全部抓取:爬取URL—>http://www.biquge.com.tw/0_66/83317.html
创建文件:D:\JSOUP\ 第六章 运蹇时乖.txt
线程:全部抓取:爬取URL—>http://www.biquge.com.tw/0_66/83318.html
创建文件:D:\JSOUP\ 第七章 白手起家(上).txt
线程:全部抓取:爬取URL—>http://www.biquge.com.tw/0_66/83319.html
创建文件:D:\JSOUP\ 第八章 白手起家(下).txt
线程:全部抓取:爬取URL—>http://www.biquge.com.tw/0_66/83320.html
创建文件:D:\JSOUP\ 第九章 江南才子.txt
线程:全部抓取:爬取URL—>http://www.biquge.com.tw/0_66/83321.html
创建文件:D:\JSOUP\ 第十章 才子招财.txt
线程:全部抓取:爬取URL—>http://www.biquge.com.tw/0_66/83322.html
创建文件:D:\JSOUP\ 第十一章 风靡江南.txt
线程:全部抓取:爬取URL—>http://www.biquge.com.tw/0_66/83323.html
创建文件:D:\JSOUP\ 第十二章 原形毕露.txt
线程:全部抓取:爬取URL—>http://www.biquge.com.tw/0_66/83324.html
创建文件:D:\JSOUP\ 第十三章 杜家危局.txt
线程:全部抓取:爬取URL—>http://www.biquge.com.tw/0_66/83325.html
创建文件:D:\JSOUP\ 第十四章 流年不利.txt
线程:全部抓取:爬取URL—>http://www.biquge.com.tw/0_66/83326.html
创建文件:D:\JSOUP\ 第十五章 化解危局(上).txt
线程:全部抓取:爬取URL—>http://www.biquge.com.tw/0_66/83327.html
创建文件:D:\JSOUP\ 第十六章 化解危局(中).txt