看網絡小說一般會攢上一波,然后導入Kindle里面去看,但是攢的多了,機械的Ctrl+C和Ctrl+V實在是OUT,所以就出現了此文。
其實Python我也是小白,用它的目的主要是它強大文本處理能力和網絡支持,以及許多好用的庫,不需要自己造輪子。而且真心比C方便啊(真是用了才知道)
分析要獲取的網頁
我要獲取的主要是3個東西:
- 文章的標題。<div id="title">正文 第一章 北靈院</div>
- 文章正文內容。<div id="content" style="line-height: 150%; color: rgb(0, 0, 0);">
- 下一章的URL。<a href="11455541.html" rel="external nofollow" >下一頁</a>
還有就是注意網頁的編碼,這個網頁的編碼是GBK,但在實際運行過程中,我用GBK會出現網頁解碼錯誤:
UnicodeDecodeError: ‘gbk' codec can't decode bytes in position 2-3: illegal multibyte sequence
所以換用了gb18030,問題就解決了,因為一般修仙網絡小說中,會出現各種王霸之氣的文字,你們懂得,所以需要更加牛逼文字庫,你們感受一下博大精深的字符編碼。
源代碼
我就知道,大家要這個,哈哈哈。
主函數
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
#主函數 if __name__ = = '__main__' : global numChapter global NOVERL NOVERL = '大主宰.txt' #NOVERL = '擇天記.txt' NOVERL = '武動乾坤.txt' if (NOVERL = = '大主宰.txt' ): textStartURL = 'http://www.bxwx8.org/b/62/62724/11455540.html' ; #大主宰第一章的URL textStartURL = 'http://www.bxwx8.org/b/62/62724/28019405.html' ; #第一千兩百三十七章 鬼大師 else : textStartURL = 'http://www.bxwx8.org/b/98/98289/17069215.html' ; #擇天記第一章URL textStartURL = 'http://www.bxwx8.org/b/98/98289/28088874.html' ; #擇天記第七十八章 合劍術 textStartURL = 'http://www.bxwx8.org/b/35/35282/5839471.html' ; #武動乾坤第一章 #textStartURL = 'http://www.bxwx8.org/b/35/35282/7620539.html';#武動乾坤 nextURL = textStartURL; isEnd = False f = open (NOVERL, 'w' , encoding = 'utf-8' ) f.close() numChapter = 0 ; while ( not isEnd): nextURL,isEnd = findNextTextURL(nextURL) print ( 'end of capture!' ) print ( '獲取到 ' + str (numChapter) + ' 章' ) |
獲取內容和下一章URL
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
#找到 下一章節的URL #獲取小說內容 def findNextTextURL(url): global numChapter global NOVERL #如果nextURL == endURL 則返回false if (NOVERL = = '大主宰.txt' ): endURL = 'http://www.bxwx8.org/b/62/62724/index.html' #大主宰 headURL = 'http://www.bxwx8.org/b/62/62724/' #大主宰 else : endURL = 'http://www.bxwx8.org/b/98/98289/index.html' #擇天記 headURL = 'http://www.bxwx8.org/b/98/98289/' #擇天記 endURL = 'http://www.bxwx8.org/b/35/35282/index.html' #武動乾坤 headURL = 'http://www.bxwx8.org/b/35/35282/' #武動乾坤 isEnd = False resp = urllib.request.urlopen(url) #處理的字符的確是gbk的,但是其中夾雜的部分特殊字符, #是gbk編碼中所沒有的如果有些特殊字符是GB18030中有的,但是是gbk中沒有的。 #則用gbk去解碼,去所不支持的字符,也比如會出錯。 #所以,此種情況,可以嘗試用和當前編碼(gbk)所兼容的但所包含字符更多的編碼(gb18030)去解碼,或許就可以了。 #allHtml = resp.read().decode('gbk')# allHtml = resp.read().decode( 'gb18030' ) # textSoup = BeautifulSoup(allHtml) #章節名 strChapter = textSoup.find( id = 'title' ).getText().split(r '【' )[ 0 ] strChapter = strChapter.split(r '(' )[ 0 ] strChapter = strChapter.replace( '正文 ' ,' ') + ' \n' numChapter = numChapter + 1 strID = '#' + str (numChapter) + '-' strChapter = strID + strChapter strChapter = strChapter + '\n------------------------------\n' + url + '\n------------------------------\n' #小說正文 strNovel = textSoup.find( id = 'content' ).getText() strNovel = strNovel.replace( ' ' , '\n' ) #除去正文中多余的第XXX章 strMatch = r "第[\u4e00-\u9fa5]+章" list2replace = re.findall(strMatch, strNovel) if list2replace: str2replace = list2replace[ 0 ] strNovel = strNovel.replace(str2replace, '') #合并章節和正文 strNovel = strChapter + strNovel + '\n------------------------------\n------------------------------\n' #寫到txt文件中 write2TXT(strNovel) #獲取下一個章節的URL nextURL = re.findall(r 'var next_page = "[\w]+.html"' , allHtml)[ 0 ] nextURL = nextURL.replace(r '"' , '') nextURL = nextURL.replace(r 'var next_page = ' , '') nextURL = headURL + nextURL print (numChapter) #章節數 print (strChapter) #章節名字 print ((nextURL)) #下一章URL if (endURL = = nextURL): isEnd = True return nextURL,isEnd |
寫入TXT
1
2
3
4
5
6
7
|
#寫到文本文件中 def write2TXT(txt): global NOVERL f = open (NOVERL, 'a' , encoding = 'utf-8' ) f.write(txt + '\n\n' ) f.close() |
結束語
三個說明:
- txt文本的編排肯定不好,而且在Kindle里面無法自動分章,多看閱讀可以,原生系統就GG了,所以下一步可以用epubBuilder這款軟件進行二次編排,輸出mobi導入你的Kindle。
- 本程序只是針對這個網站而已,但是如果網站換了,細節性代碼就得重新寫了。不過大框架還可以用。
- 網絡小說毒害有志青年,一入網文深是海,從此節操是路人,諸君且行且珍惜!
總結
以上就是本文關于Python下載網絡小說實例代碼的全部內容,希望對大家有所幫助。感興趣的朋友可以繼續參閱本站其他相關專題,如有不足之處,歡迎留言指出。感謝朋友們對本站的支持!
原文鏈接:http://blog.csdn.net/humanking7/article/details/51759965