如何用Java爬取筆趣閣(www.biquge5200.com)全部小說

目的
  現(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");

最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市磷斧,隨后出現(xiàn)的幾起案子振愿,更是在濱河造成了極大的恐慌,老刑警劉巖弛饭,帶你破解...
    沈念sama閱讀 217,277評論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件冕末,死亡現(xiàn)場離奇詭異,居然都是意外死亡侣颂,警方通過查閱死者的電腦和手機档桃,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,689評論 3 393
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來憔晒,“玉大人藻肄,你說我怎么就攤上這事【艿#” “怎么了仅炊?”我有些...
    開封第一講書人閱讀 163,624評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長澎蛛。 經(jīng)常有香客問我抚垄,道長,這世上最難降的妖魔是什么谋逻? 我笑而不...
    開封第一講書人閱讀 58,356評論 1 293
  • 正文 為了忘掉前任呆馁,我火速辦了婚禮,結果婚禮上毁兆,老公的妹妹穿的比我還像新娘雹有。我一直安慰自己构诚,他們只是感情好年栓,可當我...
    茶點故事閱讀 67,402評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著畔咧,像睡著了一般。 火紅的嫁衣襯著肌膚如雪揖膜。 梳的紋絲不亂的頭發(fā)上誓沸,一...
    開封第一講書人閱讀 51,292評論 1 301
  • 那天,我揣著相機與錄音壹粟,去河邊找鬼拜隧。 笑死,一個胖子當著我的面吹牛趁仙,可吹牛的內(nèi)容都是我干的洪添。 我是一名探鬼主播,決...
    沈念sama閱讀 40,135評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼雀费,長吁一口氣:“原來是場噩夢啊……” “哼干奢!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起盏袄,我...
    開封第一講書人閱讀 38,992評論 0 275
  • 序言:老撾萬榮一對情侶失蹤忿峻,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后貌矿,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體炭菌,經(jīng)...
    沈念sama閱讀 45,429評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡罪佳,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,636評論 3 334
  • 正文 我和宋清朗相戀三年逛漫,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片赘艳。...
    茶點故事閱讀 39,785評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡酌毡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出蕾管,到底是詐尸還是另有隱情枷踏,我是刑警寧澤,帶...
    沈念sama閱讀 35,492評論 5 345
  • 正文 年R本政府宣布掰曾,位于F島的核電站旭蠕,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏旷坦。R本人自食惡果不足惜掏熬,卻給世界環(huán)境...
    茶點故事閱讀 41,092評論 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望秒梅。 院中可真熱鬧旗芬,春花似錦、人聲如沸捆蜀。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,723評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至誊薄,卻和暖如春履恩,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背暇屋。 一陣腳步聲響...
    開封第一講書人閱讀 32,858評論 1 269
  • 我被黑心中介騙來泰國打工似袁, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人咐刨。 一個月前我還...
    沈念sama閱讀 47,891評論 2 370
  • 正文 我出身青樓昙衅,卻偏偏與公主長得像,于是被迫代替她去往敵國和親定鸟。 傳聞我的和親對象是個殘疾皇子而涉,可洞房花燭夜當晚...
    茶點故事閱讀 44,713評論 2 354