Python爬蟲入門教程02之筆趣閣小說爬取
前言
本文的文字及圖片來源于網(wǎng)絡(luò),僅供學(xué)習(xí)、交流使用,不具有任何商業(yè)用途,如有問題請及時聯(lián)系我們以作處理。
前文
基本開發(fā)環(huán)境
- Python 3.6
- Pycharm
相關(guān)模塊的使用
- request
- sparsel
安裝Python并添加到環(huán)境變量,pip安裝需要的相關(guān)模塊即可。

單章爬取

一、明確需求
爬取小說內(nèi)容保存到本地
- 小說名字
- 小說章節(jié)名字
- 小說內(nèi)容
# 第一章小說url地址 url = 'http://www.biquges.com/52_52642/25585323.html'
url = 'http://www.biquges.com/52_52642/25585323.html'
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36'
}
response = requests.get(url=url, headers=headers)
print(response.text)

請求網(wǎng)頁返回的數(shù)據(jù)中出現(xiàn)了亂碼,這就需要我們轉(zhuǎn)碼了。
加一行代碼自動轉(zhuǎn)碼。
response.encoding = response.apparent_encoding

三、解析數(shù)據(jù)

根據(jù)css選擇器可以直接提取小說標題以及小說內(nèi)容。
def get_one_novel(html_url):
# 調(diào)用請求網(wǎng)頁數(shù)據(jù)函數(shù)
response = get_response(html_url)
# 轉(zhuǎn)行成selector解析對象
selector = parsel.Selector(response.text)
# 獲取小說標題
title = selector.css('.bookname h1::text').get()
# 獲取小說內(nèi)容 返回的是list
content_list = selector.css('#content::text').getall()
# ''.join(列表) 把列表轉(zhuǎn)換成字符串
content_str = ''.join(content_list)
print(title, content_str)
if __name__ == '__main__':
url = 'http://www.biquges.com/52_52642/25585323.html'
get_one_novel(url)

四、保存數(shù)據(jù)(數(shù)據(jù)持久化)
使用常用的保存方式: with open
def save(title, content):
"""
保存小說
:param title: 小說章節(jié)標題
:param content: 小說內(nèi)容
:return:
"""
# 路徑
filename = f'{title}\\'
# os 內(nèi)置模塊,自動創(chuàng)建文件夾
if os.makedirs(filename):
os.mkdir()
# 一定要記得加后綴 .txt mode 保存方式 a 是追加保存 encoding 保存編碼
with open(filename + title + '.txt', mode='a', encoding='utf-8') as f:
# 寫入標題
f.write(title)
# 換行
f.write('\n')
# 寫入小說內(nèi)容
f.write(content)


保存一章小說,就這樣寫完了,如果想要保存整本小說呢?
整本小說爬蟲
既然爬取單章小說知道怎么爬取了,那么只需要獲取小說所有單章小說的url地址,就可以爬取全部小說內(nèi)容了。

所有的單章的url地址都在 dd 標簽當(dāng)中,但是這個url地址是不完整的,所以爬取下來的時候,要拼接url地址。
def get_all_url(html_url):
# 調(diào)用請求網(wǎng)頁數(shù)據(jù)函數(shù)
response = get_response(html_url)
# 轉(zhuǎn)行成selector解析對象
selector = parsel.Selector(response.text)
# 所有的url地址都在 a 標簽里面的 href 屬性中
dds = selector.css('#list dd a::attr(href)').getall()
for dd in dds:
novel_url = 'http://www.biquges.com' + dd
print(novel_url)
if __name__ == '__main__':
url = 'http://www.biquges.com/52_52642/index.html'
get_all_url(url)

這樣就獲取了所有的小說章節(jié)url地址了。
爬取全本完整代碼
import requests
import parsel
from tqdm import tqdm
def get_response(html_url):
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36'
}
response = requests.get(url=html_url, headers=headers)
response.encoding = response.apparent_encoding
return response
def save(novel_name, title, content):
"""
保存小說
:param title: 小說章節(jié)標題
:param content: 小說內(nèi)容
:return:
"""
filename = f'{novel_name}' + '.txt'
# 一定要記得加后綴 .txt mode 保存方式 a 是追加保存 encoding 保存編碼
with open(filename, mode='a', encoding='utf-8') as f:
# 寫入標題
f.write(title)
# 換行
f.write('\n')
# 寫入小說內(nèi)容
f.write(content)
def get_one_novel(name, novel_url):
# 調(diào)用請求網(wǎng)頁數(shù)據(jù)函數(shù)
response = get_response(novel_url)
# 轉(zhuǎn)行成selector解析對象
selector = parsel.Selector(response.text)
# 獲取小說標題
title = selector.css('.bookname h1::text').get()
# 獲取小說內(nèi)容 返回的是list
content_list = selector.css('#content::text').getall()
# ''.join(列表) 把列表轉(zhuǎn)換成字符串
content_str = ''.join(content_list)
save(name, title, content_str)
def get_all_url(html_url):
# 調(diào)用請求網(wǎng)頁數(shù)據(jù)函數(shù)
response = get_response(html_url)
# 轉(zhuǎn)行成selector解析對象
selector = parsel.Selector(response.text)
# 所有的url地址都在 a 標簽里面的 href 屬性中
dds = selector.css('#list dd a::attr(href)').getall()
# 小說名字
novel_name = selector.css('#info h1::text').get()
for dd in tqdm(dds):
novel_url = 'http://www.biquges.com' + dd
get_one_novel(novel_name, novel_url)
if __name__ == '__main__':
novel_id = input('輸入書名ID:')
url = f'http://www.biquges.com/{novel_id}/index.html'
get_all_url(url)


到此這篇關(guān)于Python爬蟲入門教程02之筆趣閣小說爬取的文章就介紹到這了,更多相關(guān)Python爬蟲筆趣閣小說爬取內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
pytorch 狀態(tài)字典:state_dict使用詳解
今天小編就為大家分享一篇pytorch 狀態(tài)字典:state_dict使用詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-01-01
TorchVision Transforms API目標檢測實例語義分割視頻類
這篇文章主要為大家介紹了TorchVision Transforms API大升級,支持目標檢測、實例/語義分割及視頻類任務(wù)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-11-11
PyCharm更換pip源、模塊安裝以及PyCharm依賴包導(dǎo)入導(dǎo)出功能
這篇文章主要給大家介紹了關(guān)于PyCharm更換pip源、模塊安裝以及PyCharm依賴包導(dǎo)入導(dǎo)出功能的相關(guān)資料,我們在使用pycharm的時候,pycharm中的虛擬環(huán)境依賴包需要導(dǎo)出成一個文件,需要的朋友可以參考下2023-11-11
python實現(xiàn)樹形打印目錄結(jié)構(gòu)
這篇文章主要為大家詳細介紹了python樹形打印目錄結(jié)構(gòu)的相關(guān)代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-03-03
python?cv2.waitKey()函數(shù)的使用
這篇文章主要介紹了python?cv2.waitKey()函數(shù)的使用方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-01-01
anaconda中Conda創(chuàng)建虛擬環(huán)境的實現(xiàn)步驟
在Anaconda中,可以使用conda命令來創(chuàng)建和管理虛擬環(huán)境,本文主要介紹了anaconda中Conda創(chuàng)建虛擬環(huán)境的實現(xiàn)步驟,具有一定的參考價值,感興趣的可以了解一下2023-12-12
Python代碼調(diào)用執(zhí)行shell踩坑解決
這篇文章主要為大家介紹了Python代碼調(diào)用執(zhí)行shell,踩過的坑解決方法,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-06-06

