看網絡小說一般會攢上一波,然后導入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,問題就解決了,因為一般修仙網絡小說中,會出現各種王霸之氣的文字,你們懂得,所以需要更加牛逼文字庫,你們感受一下博大精深的字符編碼。

我就知道,大家要這個,哈哈哈。
#主函數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#獲取小說內容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
新聞熱點
疑難解答