Python scrapy爬取小說代碼案例詳解
scrapy是目前python使用的最廣泛的爬蟲框架
架構(gòu)圖如下

解釋:
- Scrapy Engine(引擎): 負責(zé)Spider、ItemPipeline、Downloader、Scheduler中間的通訊,信號、數(shù)據(jù)傳遞等。
- Scheduler(調(diào)度器): 它負責(zé)接受引擎發(fā)送過來的Request請求,并按照一定的方式進行整理排列,入隊,當(dāng)引擎需要時,交還給引擎。
- Downloader(下載器):負責(zé)下載Scrapy Engine(引擎)發(fā)送的所有Requests請求,并將其獲取到的Responses交還給Scrapy Engine(引擎),由引擎交給Spider來處理,
- Spider(爬蟲):它負責(zé)處理所有Responses,從中分析提取數(shù)據(jù),獲取Item字段需要的數(shù)據(jù),并將需要跟進的URL提交給引擎,再次進入Scheduler(調(diào)度器),
- Item Pipeline(管道):它負責(zé)處理Spider中獲取到的Item,并進行進行后期處理(詳細分析、過濾、存儲等)的地方.
- DownloaderMiddlewares(下載中間件):你可以當(dāng)作是一個可以自定義擴展下載功能的組件。Spider Middlewares(Spider中間件):你可以理解為是一個可以自定擴展和操作引擎和Spider中間通信的功能組件(比如進入Spider的Responses;和從Spider出去的Requests
一。安裝
pip install Twisted.whl
pip install Scrapy
Twisted的版本要與安裝的python對應(yīng),https://jingyan.baidu.com/article/1709ad8027be404634c4f0e8.html
二。代碼
本實例采用xpaths解析頁面數(shù)據(jù)
按住shift-右鍵-在此處打開命令窗口
輸入scrapy startproject qiushibaike 創(chuàng)建項目
輸入scrapy genspiderqiushibaike 創(chuàng)建爬蟲
1>結(jié)構(gòu)

2>qiushibaike.py爬蟲文件
import scrapy
from scrapy.linkextractors import LinkExtractor
from scrapy.spiders.crawl import Rule, CrawlSpider
class BaiduSpider(CrawlSpider):
name = 'qiushibaike'
allowed_domains = ['qiushibaike.com']
start_urls = ['https://www.qiushibaike.com/text/']#啟始頁面
#
rules= (
Rule(LinkExtractor(restrict_xpaths=r'//a[@class="contentHerf"]'),callback='parse_item',follow=True),
Rule(LinkExtractor(restrict_xpaths=r'//ul[@class="pagination"]/li/a'),follow=True)
)
def parse_item(self, response):
title=response.xpath('//h1[@class="article-title"]/text()').extract_first().strip() #標(biāo)題
time=response.xpath(' //span[@class="stats-time"]/text()').extract_first().strip() #發(fā)布時間
content=response.xpath('//div[@class="content"]/text()').extract_first().replace(' ','\n') #內(nèi)容
score=response.xpath('//i[@class="number"]/text()').extract_first().strip() #好笑數(shù)
yield({"title":title,"content":content,"time":time,"score":score});
3>pipelines.py 數(shù)據(jù)管道[code]class QiushibaikePipeline:
class QiushibaikePipeline:
def open_spider(self,spider):#啟動爬蟲中調(diào)用
self.f=open("xiaoshuo.txt","w",encoding='utf-8')
def process_item(self, item, spider):
info=item.get("title")+"\n"+ item.get("time")+" 好笑數(shù)"+item.get("score")+"\n"+ item.get("content")+'\n'
self.f.write(info+"\n")
self.f.flush()
def close_spider(self,spider):#關(guān)閉爬蟲中調(diào)用
self.f.close()
4>settings.py
開啟ZhonghengPipeline
ITEM_PIPELINES = {
'qiushibaike.pipelines.QiushibaikePipeline': 300,
}
5>0main.py運行
from scrapy.cmdline import execute
execute('scrapy crawl qiushibaike'.split())
6>結(jié)果:
生成xiaohua.txt,里面有下載的笑話文字

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
jupyter中torch庫的安裝與虛擬環(huán)境的搭建方式
本文詳細介紹了如何在Windows系統(tǒng)上創(chuàng)建和配置PyTorch環(huán)境,包括安裝Anaconda、創(chuàng)建虛擬環(huán)境、配置鏡像源、安裝CUDA、查找和安裝PyTorch版本、安裝ipykernel以及在Jupyter Notebook中切換環(huán)境2025-02-02
Centos Python2 升級到Python3的簡單實現(xiàn)
下面小編就為大家?guī)硪黄狢entos Python2 升級到Python3的簡單實現(xiàn)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-06-06
使用基于Python的Tornado框架的HTTP客戶端的教程
這篇文章主要介紹了制作一個基于Python的Tornado框架的HTTP客戶端的教程,Tornado的異步特性使其能夠獲得很好的性能,需要的朋友可以參考下2015-04-04
Python利用pandas和matplotlib實現(xiàn)繪制柱狀折線圖
這篇文章主要為大家詳細介紹了如何使用?Python?中的?Pandas?和?Matplotlib?庫創(chuàng)建一個柱狀圖與折線圖結(jié)合的數(shù)據(jù)可視化圖表,感興趣的可以了解一下2023-11-11

