目的
現(xiàn)在很多年輕人都喜歡看網(wǎng)絡小說,網(wǎng)絡小說很容易成癮烤低,成癮之后就得大把大把的向正版網(wǎng)站去訂閱兼丰,你要不想訂閱玻孟,只能去盜版網(wǎng)站去看,盜版網(wǎng)站上各種渣渣輝鳍征、古天樂的"是兄弟就來砍我"的頁游廣告取募,特別煩人。不想花錢去正版網(wǎng)站訂閱蟆技,又不想看盜版網(wǎng)站的廣告玩敏,怎么辦?作為一個技術人质礼,就要學會去爬蟲旺聚,把盜版網(wǎng)站的數(shù)據(jù)爬過來,自己組織眶蕉,給自己看(正版網(wǎng)站砰粹,需要訂閱才能看,想要爬造挽,得花錢————目前碱璃,我還沒找到不花錢就可以爬的方法!)。(另外饭入,盜版可恥嵌器,請大家關注正版,我這個只是為了學習技術練手需要才弄的)谐丢。
觀察網(wǎng)頁特點
筆趣閣小說的全部小說欄目下爽航,包含了筆趣閣小說的全部小說(實際上并不是全部蚓让,還有一些沒有展示出來),點擊對應小說的超鏈接讥珍,就可以進入到小說的詳細目錄历极,小說的詳細目錄下,包含了小說的章節(jié)信息衷佃,點擊章節(jié)信息就可以獲得小說的內(nèi)容了趟卸。下面我們拿 龍血沸騰為例,來說:
1氏义、全部小說頁面的html
| <!doctype html> |
| | <head> |
| | <title>小說大全_全部小說目錄大全_筆趣閣</title> |
| | <meta http-equiv="Content-Type" content="text/html; charset=gbk" /> |
| | <meta name="keywords" content="小說大全" /> |
| | <meta name="description" content="筆趣閣是廣大書友最值得收藏的網(wǎng)絡小說閱讀網(wǎng)锄列,筆趣閣網(wǎng)站收錄了當前最火熱的網(wǎng)絡小說,筆趣閣5200免費提供高質(zhì)量的小說最新章節(jié)觅赊,是廣大網(wǎng)絡小說愛好者必備的小說閱讀網(wǎng)。" /> |
| | <link rel="stylesheet" type="text/css" href="[http://r.biquge5200.com/images/biquge.css](http://r.biquge5200.com/images/biquge.css)"/> |
| | <script type="text/javascript" src="[http://libs.baidu.com/jquery/1.4.2/jquery.min.js](http://libs.baidu.com/jquery/1.4.2/jquery.min.js)"></script> |
| | <script type="text/javascript" src="[http://r.biquge5200.com/images/bqg.js](http://r.biquge5200.com/images/bqg.js)"></script> |
| | </head> |
| | <body> |
| | <div id="wrapper"> |
| | <script>login();</script> |
| | <div class="header"> |
| | <div class="header_logo"> |
| | <a href="[http://www.biquge5200.com](http://www.biquge5200.com/)">筆趣閣</a> |
| | </div> |
| | <script>bqg_panel();</script> |
| | </div> |
| | <div class="nav"> |
| | <ul> |
| | <li><a href="[http://www.biquge5200.com/](http://www.biquge5200.com/)">首頁</a></li> |
| | <li><a rel="nofollow" href="[http://www.biquge5200.com/modules/article/bookcase.php](http://www.biquge5200.com/modules/article/bookcase.php)">我的書架</a></li> |
| | <li><a href="[http://www.biquge5200.com/xuanhuanxiaoshuo/](http://www.biquge5200.com/xuanhuanxiaoshuo/)">玄幻小說</a></li> |
| | <li><a href="[http://www.biquge5200.com/xiuzhenxiaoshuo/](http://www.biquge5200.com/xiuzhenxiaoshuo/)">修真小說</a></li> |
| | <li><a href="[http://www.biquge5200.com/dushixiaoshuo/](http://www.biquge5200.com/dushixiaoshuo/)">都市小說</a></li> |
| | <li><a href="[http://www.biquge5200.com/chuanyuexiaoshuo/](http://www.biquge5200.com/chuanyuexiaoshuo/)">穿越小說</a></li> |
| | <li><a href="[http://www.biquge5200.com/wangyouxiaoshuo/](http://www.biquge5200.com/wangyouxiaoshuo/)">網(wǎng)游小說</a></li> |
| | <li><a href="[http://www.biquge5200.com/kehuanxiaoshuo/](http://www.biquge5200.com/kehuanxiaoshuo/)">科幻小說</a></li> |
| | <li><a href="[http://www.biquge5200.com/paihangbang/](http://www.biquge5200.com/paihangbang/)">排行榜單</a></li> |
| | <li><a href="[http://www.biquge5200.com/xiaoshuodaquan/](http://www.biquge5200.com/xiaoshuodaquan/)">全部小說</a></li> |
| | </ul> |
| | </div> |
| | <center><script>list1();</script></center> |
| | <div class="MessageDiv"> |
| | <b>提示:本站收錄的全部小說均在此頁琼稻, 推薦使用Ctrl+F 來查找小說吮螺。</b> |
| | </div> |
| | <div class="dahengfu"><script>list();</script></div> |
| | <script>dingbu();</script> |
| | <div id="main"> |
| | <div class="novellist"> |
| | <h2>奇幻、玄幻小說大全列表</h2> |
| | <ul> |
| | |
| | <li><a href="[http://www.biquge5200.com/14_14621/](http://www.biquge5200.com/14_14621/)">斗神狂飆</a><strong style="color:#FF0000">(完本)</strong></li> |
| | |
| | <li><a href="[http://www.biquge5200.com/14_14620/](http://www.biquge5200.com/14_14620/)">龍血沸騰</a></li> |
| | |
| | <li><a href="[http://www.biquge5200.com/14_14613/](http://www.biquge5200.com/14_14613/)">軟妹異界游</a></li> |
| | |
| | <li><a href="[http://www.biquge5200.com/14_14602/](http://www.biquge5200.com/14_14602/)">至尊仙魔</a></li> |
| | |
| | <li><a href="[http://www.biquge5200.com/14_14601/](http://www.biquge5200.com/14_14601/)">丹武主宰</a></li> |
| | |
| | <li><a href="[http://www.biquge5200.com/14_14598/](http://www.biquge5200.com/14_14598/)">斗仙機</a></li> |
| | |
| | <li><a href="[http://www.biquge5200.com/14_14588/](http://www.biquge5200.com/14_14588/)">萬妖至皇</a></li> |
| | |
| | <li><a href="[http://www.biquge5200.com/14_14567/](http://www.biquge5200.com/14_14567/)">殺戮武皇</a></li> |
| <li><a href="[http://www.biquge5200.com/3_3316/](http://www.biquge5200.com/3_3316/)">最強超級英雄</a></li> |
| | |
| | <li><a href="[http://www.biquge5200.com/3_3304/](http://www.biquge5200.com/3_3304/)">我的主神是團長</a></li> |
| | |
| | <li><a href="[http://www.biquge5200.com/3_3293/](http://www.biquge5200.com/3_3293/)">陽世鬼差</a></li> |
| | |
| | <li><a href="[http://www.biquge5200.com/3_3276/](http://www.biquge5200.com/3_3276/)">神的世界編輯器</a></li> |
| |
|
| | </ul></div> |
| | <div class="clear"></div> |
| | </div> |
| | <div class="dahengfu"><script type="text/javascript">bottom();</script></div> |
| | |
| | <div class="footer"> |
| | <div class="footer_link"></div> |
| | <div class="footer_cont"> |
| | <script>footer();right();dl();</script> |
| | </div> |
| | </div> |
| | </div> |
| | </body> |
| | </html> |
我們需要的小說詳細頁就在<li><a href="[http://www.biquge5200.com/14_14620/](http://www.biquge5200.com/14_14620/)">龍血沸騰</a></li>
里面帕翻,而
http://www.biquge5200.com/14_14620/就是小說的詳細頁面的URL了鸠补,使用正則表達式<li><a href="(?<detailurl>[\s\S]+?)">(?<bookname>[\s\S]+?)</a>
就可以獲得小說的標題及詳細頁鏈接了,但是會得到標題頁的信息嘀掸,后面要清理掉紫岩,另外還有紅色強調(diào)的臟數(shù)據(jù),后續(xù)也要清理掉睬塌。
好了泉蝌,廢話不說了,上代碼揩晴!
1勋陪、導入需要的依賴的jar包,這個小程序是通過httpclient來實現(xiàn)的硫兰,所以要導入httpclient相關的jar包诅愚,maven依賴如下
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.3</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.9</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.4.6</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.8.0</version>
</dependency>
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>3.2.1</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>net.sf.ezmorph</groupId>
<artifactId>ezmorph</artifactId>
<version>1.0.6</version>
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<version>1.7.5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/oro/oro -->
<dependency>
<groupId>oro</groupId>
<artifactId>oro</artifactId>
<version>2.0.8</version>
</dependency>
<!-- https://mvnrepository.com/artifact/xom/xom -->
<dependency>
<groupId>xom</groupId>
<artifactId>xom</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<!-- jsoup HTML parser library @ https://jsoup.org/ -->
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.11.2</version>
</dependency>
為了過濾掉標題頁的信息,我們使用Jsoup劫映,直接獲取<div class="novellist">的Element信息违孝,代碼如下:
try {
Document document = Jsoup.connect(Constants.BIQUGE5200_URL).get(); //public static final String BIQUGE5200_URL = "http://www.biquge5200.com/xiaoshuodaquan/";
Elements element = document.getElementsByClass(Constants.BIQUGE5200_NOVELLIST_STR); //public static final String BIQUGE5200_NOVELLIST_STR = "novellist";
} catch (IOException e) {
e.printStackTrace();
}
獲取到書籍信息列表的信息后,再通過正則表達式來獲取書籍及對應的詳情頁url信息泳赋,代碼如下:
Pattern pattern = Pattern.compile(Constants.BIQUGE5200_ALL_GREP_RULE);
try {
Document document = Jsoup.connect(Constants.BIQUGE5200_URL).get();
Elements element = document.getElementsByClass(Constants.BIQUGE5200_NOVELLIST_STR); //public static final String BIQUGE5200_ALL_GREP_RULE = "<li><a href=\"(?<detailurl>[\\s\\S]+?)\">(?<bookname>[\\s\\S]+?)</a>"; //筆趣閣5200全部小說頁獲取小說名稱及url的正則表達式
Matcher matcher = pattern.matcher(element.toString());
while(matcher.find()){
String novelname = matcher.group(2);
String novelurl = matcher.group(1);
}
} catch (IOException e) {
e.printStackTrace();
}
得到章節(jié)URL之后雌桑,根據(jù)章節(jié)URL來獲取章節(jié)內(nèi)容,章節(jié)內(nèi)容的頁面很簡單,只需要通過jsoup獲取<div id="content">
的內(nèi)容即可,代碼如下:
Pattern pattern = Pattern.compile(Constants.BIQUGE5200_ALL_GREP_RULE);
Pattern detail_pattern = Pattern.compile(Constants.BIQUGE5200_CHAPTER_GREP_RULE);
try {
Document document = Jsoup.connect(Constants.BIQUGE5200_URL).get();
Elements element = document.getElementsByClass(Constants.BIQUGE5200_NOVELLIST_STR);
Matcher matcher = pattern.matcher(element.toString());
while(matcher.find()){
String novelname = matcher.group(2);
String novelurl = matcher.group(1);
Document novel_detail_document = Jsoup.connect(novelurl).get();
Element novel_detail_element = novel_detail_document.getElementById("list");
Matcher detail = detail_pattern.matcher(novel_detail_element.toString());
while(detail.find()){
String chaptername = detail.group(2);
String chapterurl = detail.group(1);
Document content_document = Jsoup.connect(chapterurl).get();
String content = content_document.getElementById("content").html().toString().replace("<br>", "");
}
}
Thread.sleep(200l);
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
最后將獲取到的數(shù)據(jù)寫入文件中祖今,整體代碼如下:
Constants.java
public class Constants {
public static final String BIQUGE5200_URL = "http://www.biquge5200.com/xiaoshuodaquan/"; //筆趣閣5200 全部小說頁的URL
public static final String BIQUGE5200_ALL_GREP_RULE = "<li><a href=\"(?<detailurl>[\\s\\S]+?)\">(?<bookname>[\\s\\S]+?)</a>"; //筆趣閣5200全部小說頁獲取小說名稱及url的正則表達式
public static final String BIQUGE5200_CHAPTER_GREP_RULE = "<dd>\\s*<a href=\"(?<chapterurl>[\\s\\S]+?)\">(?<chaptername>[\\s\\S]+?)</a>\\s*</dd>"; //筆趣閣5200小說詳細頁獲取
public static final String BIQUGE5200_CONTENT_GREP_RULE = "";
public static final String BIQUGE5200_NOVELLIST_STR = "novellist";
public static final String BIQUGE5200_RESULT_DIR = "F:\\opt";
}
BiqugeSpider.java的代碼:
public class BiqugeSpider {
public static void main(String []args){
Pattern pattern = Pattern.compile(Constants.BIQUGE5200_ALL_GREP_RULE);
Pattern detail_pattern = Pattern.compile(Constants.BIQUGE5200_CHAPTER_GREP_RULE);
try {
Document document = Jsoup.connect(Constants.BIQUGE5200_URL).get();
Elements element = document.getElementsByClass(Constants.BIQUGE5200_NOVELLIST_STR);
Matcher matcher = pattern.matcher(element.toString());
while(matcher.find()){
String novelname = matcher.group(2);
String novelurl = matcher.group(1);
File file = new File(Constants.BIQUGE5200_RESULT_DIR + "\\" + novelname);
if(file.isDirectory()||file.mkdir()){
Document novel_detail_document = Jsoup.connect(novelurl).get();
Element novel_detail_element = novel_detail_document.getElementById("list");
Matcher detail = detail_pattern.matcher(novel_detail_element.toString());
while(detail.find()){
String chaptername = detail.group(2);
String chapterurl = detail.group(1);
Document content_document = Jsoup.connect(chapterurl).get();
String content = content_document.getElementById("content").html().toString().replace("<br>", "");
String chapter_dir = Constants.BIQUGE5200_RESULT_DIR + "\\" + novelname + "\\" + chaptername + ".txt";
writeText(chapter_dir, content);
}
}
Thread.sleep(200l);
}
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
}
public static boolean writeText(String path, String text){
File file = new File(path);
boolean ret = true;
try (FileOutputStream fos = new FileOutputStream(file);
OutputStreamWriter osw = new OutputStreamWriter(fos, Charset.forName("UTF-8"));
BufferedWriter bw = new BufferedWriter(osw, 4096)
){
bw.append(text);
bw.flush();
} catch (IOException e) {
ret = false;
}
return ret;
}
}
遺留:
單線程跑筹燕,運行很慢,接下來要弄成多線程的,另外,目前拿到的小說撒踪,還不是全部的小說过咬,還有一些是隱藏起來了,沒有拿到制妄,如果有需要的話掸绞,可以從起點中文網(wǎng)、創(chuàng)世中文網(wǎng)耕捞、縱橫中文網(wǎng)衔掸、中文在線等正版網(wǎng)站中拿到書籍名稱后,再到筆趣閣中來搜索俺抽,就可以拿到內(nèi)容了敞映。
搜索的URL組成如下:
http://www.biquge5200.com/modules/article/search.php?searchkey=書名
注意書名需要進行編碼URLEncoder.encode("書名", "UTF-8");