濮阳杆衣贸易有限公司

主頁(yè) > 知識(shí)庫(kù) > Python爬蟲(chóng)入門(mén)教程02之筆趣閣小說(shuō)爬取

Python爬蟲(chóng)入門(mén)教程02之筆趣閣小說(shuō)爬取

熱門(mén)標(biāo)簽:萬(wàn)利達(dá)百貨商場(chǎng)地圖標(biāo)注 熱門(mén)電銷(xiāo)機(jī)器人 上海企業(yè)外呼系統(tǒng) 電話機(jī)器人哪里有賣(mài) okcc外呼系統(tǒng)怎么調(diào)速度 智能機(jī)器人電銷(xiāo)神器 河南虛擬外呼系統(tǒng)公司 惠州龍門(mén)400電話要怎么申請(qǐng) 外呼電信系統(tǒng)

前言

本文的文字及圖片來(lái)源于網(wǎng)絡(luò),僅供學(xué)習(xí)、交流使用,不具有任何商業(yè)用途,如有問(wèn)題請(qǐng)及時(shí)聯(lián)系我們以作處理。

前文

01、python爬蟲(chóng)入門(mén)教程01:豆瓣Top電影爬取

基本開(kāi)發(fā)環(huán)境

  • Python 3.6
  • Pycharm

相關(guān)模塊的使用

  • request
  • sparsel

安裝Python并添加到環(huán)境變量,pip安裝需要的相關(guān)模塊即可。

單章爬取

一、明確需求

爬取小說(shuō)內(nèi)容保存到本地

  • 小說(shuō)名字
  • 小說(shuō)章節(jié)名字
  • 小說(shuō)內(nèi)容
# 第一章小說(shuō)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)

請(qǐng)求網(wǎng)頁(yè)返回的數(shù)據(jù)中出現(xiàn)了亂碼,這就需要我們轉(zhuǎn)碼了。

加一行代碼自動(dòng)轉(zhuǎn)碼。

response.encoding = response.apparent_encoding

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

根據(jù)css選擇器可以直接提取小說(shuō)標(biāo)題以及小說(shuō)內(nèi)容。

def get_one_novel(html_url):
 # 調(diào)用請(qǐng)求網(wǎng)頁(yè)數(shù)據(jù)函數(shù)
 response = get_response(html_url)
 # 轉(zhuǎn)行成selector解析對(duì)象
 selector = parsel.Selector(response.text)
 # 獲取小說(shuō)標(biāo)題
 title = selector.css('.bookname h1::text').get()
 # 獲取小說(shuō)內(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):
 """
 保存小說(shuō)
 :param title: 小說(shuō)章節(jié)標(biāo)題
 :param content: 小說(shuō)內(nèi)容
 :return: 
 """
 # 路徑
 filename = f'{title}\\'
 # os 內(nèi)置模塊,自動(dòng)創(chuàng)建文件夾
 if os.makedirs(filename):
 os.mkdir()
 # 一定要記得加后綴 .txt mode 保存方式 a 是追加保存 encoding 保存編碼
 with open(filename + title + '.txt', mode='a', encoding='utf-8') as f:
 # 寫(xiě)入標(biāo)題
 f.write(title)
 # 換行
 f.write('\n')
 # 寫(xiě)入小說(shuō)內(nèi)容
 f.write(content)


保存一章小說(shuō),就這樣寫(xiě)完了,如果想要保存整本小說(shuō)呢?

整本小說(shuō)爬蟲(chóng)

既然爬取單章小說(shuō)知道怎么爬取了,那么只需要獲取小說(shuō)所有單章小說(shuō)的url地址,就可以爬取全部小說(shuō)內(nèi)容了。

所有的單章的url地址都在 dd 標(biāo)簽當(dāng)中,但是這個(gè)url地址是不完整的,所以爬取下來(lái)的時(shí)候,要拼接url地址。

def get_all_url(html_url):
 # 調(diào)用請(qǐng)求網(wǎng)頁(yè)數(shù)據(jù)函數(shù)
 response = get_response(html_url)
 # 轉(zhuǎn)行成selector解析對(duì)象
 selector = parsel.Selector(response.text)
 # 所有的url地址都在 a 標(biāo)簽里面的 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)

這樣就獲取了所有的小說(shuō)章節(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):
 """
 保存小說(shuō)
 :param title: 小說(shuō)章節(jié)標(biāo)題
 :param content: 小說(shuō)內(nèi)容
 :return:
 """
 filename = f'{novel_name}' + '.txt'
 # 一定要記得加后綴 .txt mode 保存方式 a 是追加保存 encoding 保存編碼
 with open(filename, mode='a', encoding='utf-8') as f:
 # 寫(xiě)入標(biāo)題
 f.write(title)
 # 換行
 f.write('\n')
 # 寫(xiě)入小說(shuō)內(nèi)容
 f.write(content)


def get_one_novel(name, novel_url):
 # 調(diào)用請(qǐng)求網(wǎng)頁(yè)數(shù)據(jù)函數(shù)
 response = get_response(novel_url)
 # 轉(zhuǎn)行成selector解析對(duì)象
 selector = parsel.Selector(response.text)
 # 獲取小說(shuō)標(biāo)題
 title = selector.css('.bookname h1::text').get()
 # 獲取小說(shuō)內(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)用請(qǐng)求網(wǎng)頁(yè)數(shù)據(jù)函數(shù)
 response = get_response(html_url)
 # 轉(zhuǎn)行成selector解析對(duì)象
 selector = parsel.Selector(response.text)
 # 所有的url地址都在 a 標(biāo)簽里面的 href 屬性中
 dds = selector.css('#list dd a::attr(href)').getall()
 # 小說(shuō)名字
 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('輸入書(shū)名ID:')
 url = f'http://www.biquges.com/{novel_id}/index.html'
 get_all_url(url)


到此這篇關(guān)于Python爬蟲(chóng)入門(mén)教程02之筆趣閣小說(shuō)爬取的文章就介紹到這了,更多相關(guān)Python爬蟲(chóng)筆趣閣小說(shuō)爬取內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

您可能感興趣的文章:
  • python 爬取國(guó)內(nèi)小說(shuō)網(wǎng)站
  • python 爬取小說(shuō)并下載的示例
  • python爬取”頂點(diǎn)小說(shuō)網(wǎng)“《純陽(yáng)劍尊》的示例代碼
  • Python爬取365好書(shū)中小說(shuō)代碼實(shí)例
  • Python實(shí)現(xiàn)的爬取小說(shuō)爬蟲(chóng)功能示例
  • Python scrapy爬取起點(diǎn)中文網(wǎng)小說(shuō)榜單
  • python爬蟲(chóng)之爬取筆趣閣小說(shuō)升級(jí)版

標(biāo)簽:淮安 周口 秦皇島 周口 綏化 百色 合肥 綿陽(yáng)

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Python爬蟲(chóng)入門(mén)教程02之筆趣閣小說(shuō)爬取》,本文關(guān)鍵詞  Python,爬蟲(chóng),入門(mén)教程,之筆,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《Python爬蟲(chóng)入門(mén)教程02之筆趣閣小說(shuō)爬取》相關(guān)的同類(lèi)信息!
  • 本頁(yè)收集關(guān)于Python爬蟲(chóng)入門(mén)教程02之筆趣閣小說(shuō)爬取的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    平利县| 北流市| 芜湖市| 松原市| 健康| 万荣县| 嵊泗县| 武强县| 井研县| 林州市| 洱源县| 鄂伦春自治旗| 阆中市| 曲阜市| 侯马市| 资源县| 石阡县| 芮城县| 读书| 都安| 黑河市| 嘉荫县| 娄底市| 衡阳县| 崇仁县| 孟州市| 黑河市| 云林县| 灵武市| 夏津县| 邯郸县| 察隅县| 海林市| 皋兰县| 山东省| 银川市| 萍乡市| 南充市| 夏河县| 朝阳区| 舟曲县|