Python實現(xiàn)的爬取小說爬蟲功能示例
本文實例講述了Python實現(xiàn)的爬取小說爬蟲功能。分享給大家供大家參考,具體如下:
想把頂點小說網(wǎng)上的一篇持續(xù)更新的小說下下來,就寫了一個簡單的爬蟲,可以爬取爬取各個章節(jié)的內(nèi)容,保存到txt文檔中,支持持續(xù)更新保存。需要配置一些信息,設(shè)置文檔保存路徑,書名等。寫著玩,可能不大規(guī)范。
# coding=utf-8 import requests from lxml import etree from urllib.parse import urljoin import re import os # 獲取頁面,并返回解析整理好的文本 def get_page(url): response = requests.get(url, headers=header) set_encoding(response) text = parse_page(response.text) return text # 解析頁面,將當(dāng)前頁面中的文字篩選出來 def parse_page(html): title = re.findall('<div class="bookname">\s+<h1>(.+?)</h1>', html)[0] content = re.findall('div id="content">(.*?)</div>', html, re.S)[0] content = content.replace('<br />', '').replace(' ', ' ').replace('\r\n\r\n', '\r\n') content = title + '\r\n' + content + '\r\n\r\n' return content # 將文本追加到file_path對應(yīng)的txt中 def save_page(path, text): with open(path, 'a', encoding='utf-8') as f: f.write(text) # 設(shè)置對response得到文本的解析編碼為'gbk' def set_encoding(response): response.encoding = 'gbk' # 從配置文件中獲取當(dāng)前保存的鏈接總數(shù) def get_current_chapters_count(path): # (1)第一次讀配置文件可能沒有創(chuàng)建,所以要支持沒有文件創(chuàng)建文件的功能(2)如果文件存在,則不能清空,參考http://www.dbjr.com.cn/article/158740.htm with open(path, 'a+') as f: f.seek(0) res = f.read() if res == '': return 0 else: return int(res) # 將保存的鏈接總數(shù)保存到配置文件中 def set_current_chapters_count(path, count): with open(path, 'w') as f: f.write(str(count)) # 需要配置的字典 config_dic = dict(start_url='http://www.booktxt.net/2_2220/', # 待下載小說的章節(jié)首頁 latest_item=9, # 列出的所有章節(jié)鏈接中,前面幾個鏈接為最新章節(jié),一般為9個,爬取時避免與最后部分重復(fù),所以前面9個鏈接不爬取 bookname='贅婿', # 待下載的小說名 folder_path='D:\\') #保存位置 domain = 'http://www.booktxt.net' # 頂點網(wǎng)域名 if __name__ == '__main__': chapter_url_list = [] response = requests.get(config_dic['start_url'], headers=header) set_encoding(response) html = etree.HTML(response.text) chapters = html.xpath('//dd') print('所有鏈接' + str(len(chapters))) chapters = chapters[config_dic['latest_item']:] # 前9章為最新章節(jié),后面還會重復(fù),這里去掉 print('不重復(fù)有效章節(jié)鏈接' + str(len(chapters))) folder_path = config_dic['folder_path'] + config_dic['bookname'] if not os.path.exists(folder_path): os.mkdir(folder_path) file_path = folder_path + '\\' + config_dic['bookname'] + '.txt' config_file_path = folder_path + '\\' + 'config.txt' print('小說存儲路徑為:' + file_path) print('配置文件存儲路徑為:' + config_file_path) saved_count = get_current_chapters_count(config_file_path) # 獲取目前保存的小說中已經(jīng)包含的章節(jié)數(shù) print('當(dāng)前' + file_path + '中已經(jīng)保存的章節(jié)總數(shù)' + str(saved_count)) if saved_count < len(chapters): # 說明有更新 set_current_chapters_count(config_file_path, len(chapters)) print('共更新 ' + str(len(chapters) - saved_count) + ' 章') for c in chapters[saved_count:]: # 從上次保存的位置開始繼續(xù)保存 url = c.xpath('a/@href')[0] url = urljoin(domain, url) txt = c.xpath('a/text()')[0] chapter_url_list.append(url) print(url) print(txt) save_page(file_path, get_page(url)) else: print('小說還沒有更新哦')
更多關(guān)于Python相關(guān)內(nèi)容可查看本站專題:《Python Socket編程技巧總結(jié)》、《Python正則表達(dá)式用法總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進(jìn)階經(jīng)典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對大家Python程序設(shè)計有所幫助。
相關(guān)文章
selenium+python實現(xiàn)基本自動化測試的示例代碼
這篇文章主要介紹了selenium+python實現(xiàn)基本自動化測試的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01Pycharm中切換pytorch的環(huán)境和配置的教程詳解
這篇文章主要介紹了Pycharm中切換pytorch的環(huán)境和配置,本文給大家介紹的非常詳細(xì),對大家的工作或?qū)W習(xí)具有一定的參考借鑒價值,需要的朋友可以參考下2020-03-03Python制作旋轉(zhuǎn)花燈祝大家元宵節(jié)快樂(實例代碼)
一年一度的元宵節(jié)來臨,小編在這里祝大家2022元宵節(jié)快樂,今天小編給大家分享一篇教程關(guān)于Python制作旋轉(zhuǎn)花燈祝大家元宵節(jié)快樂,代碼簡單易懂,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧2022-02-02python之線程池map()方法傳遞多參數(shù)list
這篇文章主要介紹了python之線程池map()方法傳遞多參數(shù)list問題,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-03-03Python面向?qū)ο笕筇卣?封裝、繼承、多態(tài)
這篇文章主要介紹了Python面向?qū)ο笕筇卣?封裝、繼承、多態(tài),下面文章圍繞Python面向?qū)ο笕筇卣鞯南嚓P(guān)資料展開具體內(nèi)容,需要的朋友可以參考一下,希望對大家有所幫助2021-11-11