介紹
使用scrapy輕松建構一個可以爬取完整小說的爬蟲
目錄
-
- 介紹
- 1、建立項目
- 2、建立爬蟲腳本
- 3、分析要爬取的小說
- 4、爬取并解析資料
- 5、接收并儲存資料
- 6、配置修改
- 7、編寫啟動腳本
- 8、效果檢視
1、建立項目
在
test01目錄
下執行指令
scrapy startproject xiaoshuospider
,建立一個名為
xiaoshuospider
的爬蟲
D:\3.dev\pyworkspace\scraw\test01>scrapy startproject xiaoshuospider
New Scrapy project 'xiaoshuospider', using template directory 'c:\users\flxk\appdata\local\programs\python\python36\lib\site-packages\scrapy\templates\project', created in:
D:\3.dev\pyworkspace\scraw\test01\xiaoshuospider
You can start your first spider with:
cd xiaoshuospider
scrapy genspider example example.com
目錄結構如下

2、建立爬蟲腳本
在
xiaoshuospider
下執行指令
scrapy genspider quanben quanben.net
,生成
quanben.py
爬蟲檔案
D:\3.dev\pyworkspace\scraw\test01\xiaoshuospider>scrapy genspider quanben quanben.net
Created spider 'quanben' using template 'basic' in module:
xiaoshuospider.spiders.quanben
初始内容如下
# -*- coding: utf-8 -*-
import scrapy
class QuanbenSpider(scrapy.Spider):
# 爬蟲名稱
name = 'quanben'
# 爬蟲允許通路的域
allowed_domains = ['quanben.net']
# 初始通路位址(手動修改為自己想要的位址)
start_urls = ['https://www.quanben.net/8/8583/4296044.html']
def parse(self, response):
pass
3、分析要爬取的小說
結合xpath插件(xpath安裝與使用),我們可以提取到小說的章節名稱、内容和下一章節url位址
4、爬取并解析資料
在
quanben.py
檔案中編寫請求和資料解析邏輯
# -*- coding: utf-8 -*-
import scrapy
class QuanbenSpider(scrapy.Spider):
# 爬蟲名稱
name = 'quanben'
# 爬蟲允許通路的域
allowed_domains = ['quanben.net']
# 初始通路位址(手動修改為自己想要的位址)
start_urls = ['https://www.quanben.net/8/8583/4296044.html']
def parse(self, response):
# 章節标題
title = response.xpath('//h1/text()').extract_first()
# 内容
content = response.xpath('string(//div[@id="BookText"])').extract_first().strip().replace(' ','\n')
# 下一章節位址
next_url = response.xpath('//div[@class="link xb"]/a[3]/@href').extract_first()
# 通過yield,将這個title、content傳遞給 pipelines.py做進一步處理
yield {
'title': title,
'content': content
}
# 通過yield,獲得下一個url,并在請求完成後調用該對象的回調函數
yield scrapy.Request(response.urljoin(next_url), callback=self.parse)
5、接收并儲存資料
在
pipelines.py
檔案中編寫資料持久化邏輯
# -*- coding: utf-8 -*-
# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html
class XiaoshuospiderPipeline(object):
# 打開檔案
def open_spider(self, spider):
self.filename = open('xiaoshuo.txt', 'w', encoding='utf-8')
def process_item(self, item, spider):
# 爬取标題
info = item['title'] + '\n'
# 爬取完整内容
# info = item['title'] + '\n' + item['content'] + '\n' + '---------------------分割線----------------------' + '\n'
# 寫入檔案
self.filename.write(info)
self.filename.flush()
return item
# 關閉檔案
def close_spider(self, spider):
self.filename.close()
6、配置修改
在
settings.py
檔案中的需要修改的配置資訊
# 設定浏覽器User-Agent請求頭
USER_AGENT = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.106 Safari/537.36'
# 是否遵循爬蟲協定
ROBOTSTXT_OBEY = False
# 每隔2s請求一次
DOWNLOAD_DELAY = 2
# 開啟pipelines
ITEM_PIPELINES = {
'xiaoshuospider.pipelines.XiaoshuospiderPipeline': 300,
}
7、編寫啟動腳本
在
xiaoshuospider
目錄下建立
start.py
腳本,編寫如下啟動指令
from scrapy.cmdline import execute
# 啟動指令
execute('scrapy crawl quanben'.split())
8、效果檢視
執行腳本後,會生成
xiaoshuo.txt
檔案儲存爬取的小說内容
第一章 雪鷹領
第二章 超凡
第三章 分離
第四章 兄弟
第五章 槍法
第六章 修煉
...