python爬蟲scrapy基于CrawlSpider類的全站數(shù)據(jù)爬取示例解析
一、CrawlSpider類介紹
1.1 引入
使用scrapy框架進(jìn)行全站數(shù)據(jù)爬取可以基于Spider類,也可以使用接下來用到的CrawlSpider類?;赟pider類的全站數(shù)據(jù)爬取之前舉過栗子,感興趣的可以康康
scrapy基于CrawlSpider類的全站數(shù)據(jù)爬取
1.2 介紹和使用
1.2.1 介紹
CrawlSpider
是Spider的一個子類,因此CrawlSpider
除了繼承Spider的特性和功能外,還有自己特有的功能,主要用到的是 LinkExtractor()
和rules = (Rule(LinkExtractor(allow=r'Items/'), callback='parse_item', follow=True),)
LinkExtractor()
:鏈接提取器
LinkExtractor()
接受response對象,并根據(jù)allow
對應(yīng)的正則表達(dá)式提取響應(yīng)對象中的鏈接
link = LinkExtractor( # Items只能是一個正則表達(dá)式,會提取當(dāng)前頁面中滿足該"正則表達(dá)式"的url allow=r'Items/' )
rules = (Rule(link, callback='parse_item', follow=True),)
:規(guī)則解析器
按照指定規(guī)則從鏈接提取器中提取到的鏈接中解析網(wǎng)頁數(shù)據(jù)
link:是一個LinkExtractor()對象,指定鏈接提取器
callback:回調(diào)函數(shù),指定規(guī)則解析器(解析方法)解析數(shù)據(jù)
follow:是否將鏈接提取器繼續(xù)作用到鏈接提取器提取出的鏈接網(wǎng)頁中
import scrapy # 導(dǎo)入相關(guān)的包 from scrapy.linkextractors import LinkExtractor from scrapy.spiders import CrawlSpider, Rule class TextSpider(CrawlSpider): name = 'text' allowed_domains = ['www.xxx.com'] start_urls = ['http://www.xxx.com/'] # 鏈接提取器,從接受到的response對象中,根據(jù)item正則表達(dá)式提取頁面中的鏈接 link = LinkExtractor(allow=r'Items/') link2 = LinkExtractor(allow=r'Items/') # 規(guī)則解析器,根據(jù)callback將鏈接提取器提取到的鏈接進(jìn)行數(shù)據(jù)解析 # follow為true,則表示將鏈接提取器繼續(xù)作用到鏈接提取器所提取到的鏈接頁面中 # 故:在我們提取多頁數(shù)據(jù)時,若第一頁對應(yīng)的網(wǎng)頁中包含了第2,3,4,5頁的鏈接, # 當(dāng)跳轉(zhuǎn)到第5頁時,第5頁又包含了第6,7,8,9頁的鏈接, # 令follow=True,就可以持續(xù)作用,從而提取到所有頁面的鏈接 rules = (Rule(link, callback='parse_item', follow=True), Rule(link2,callback='parse_content',follow=False)) # 鏈接提取器link使用parse_item解析數(shù)據(jù) def parse_item(self, response): item = {} yield item # 鏈接提取器link2使用parse_content解析數(shù)據(jù) def parse_content(self, response): item = {} yield item
1.2.2 使用
創(chuàng)建爬蟲文件:除了創(chuàng)建爬蟲文件不同外,創(chuàng)建項(xiàng)目和運(yùn)行爬蟲使用的命令和基于Spider類使用的命令相同
scrapy genspider crawl -t spiderName www.xxx.com
二、案例:古詩文網(wǎng)全站數(shù)據(jù)爬取
爬取古詩文網(wǎng)首頁古詩的標(biāo)題,以及每一首詩詳情頁古詩的標(biāo)題和內(nèi)容。
最后將從詳情頁提取到的古詩標(biāo)題和內(nèi)容進(jìn)行持久化存儲
2.1 爬蟲文件
import scrapy from scrapy.linkextractors import LinkExtractor from scrapy.spiders import CrawlSpider, Rule from gushiPro.items import GushiproItem,ContentItem class GushiSpider(CrawlSpider): name = 'gushi' #allowed_domains = ['www.xxx.com'] start_urls = ['https://www.gushiwen.org/'] # 鏈接提取器:只能使用正則表達(dá)式,提取當(dāng)前頁面的滿足allow條件的鏈接 link = LinkExtractor(allow=r'/default_\d+\.aspx') # 鏈接提取器,提取所有標(biāo)題對應(yīng)的詳情頁url content_link = LinkExtractor(allow=r'cn/shiwenv_\w+\.aspx') rules = ( # 規(guī)則解析器,需要解析所有的頁面,所有follow=True Rule(link, callback='parse_item', follow=True), # 不需要寫follow,因?yàn)槲覀冎恍枰馕鲈斍轫撝械臄?shù)據(jù),而不是詳情頁中的url Rule(content_link, callback='content_item'), ) # 解析當(dāng)前頁面的標(biāo)題 def parse_item(self, response): p_list = response.xpath('//div[@class="sons"]/div[1]/p[1]') for p in p_list: title = p.xpath('./a//text()').extract_first() item = GushiproItem() item['title'] = title yield item # 解析詳情頁面的標(biāo)題和內(nèi)容 def content_item(self,response): # //div[@id="sonsyuanwen"]/div[@class="cont"]/div[@class="contson"] # 解析詳情頁面的內(nèi)容 content = response.xpath('//div[@id="sonsyuanwen"]/div[@class="cont"]/div[@class="contson"]//text()').extract() content = "".join(content) # # 解析詳情頁面的標(biāo)題 title = response.xpath('//div[@id="sonsyuanwen"]/div[@class="cont"]/h1/text()').extract_first() # print("title:"+title+"\ncontent:"+content) item = ContentItem() item["content"] = content item["title"] = title # 將itme對象傳給管道 yield item
2.2 item文件
import scrapy # 不同的item類是獨(dú)立的,他們可以創(chuàng)建不同的item對象 class GushiproItem(scrapy.Item): # define the fields for your item here like: # name = scrapy.Field() title = scrapy.Field() class ContentItem(scrapy.Item): title = scrapy.Field() content = scrapy.Field()
2.3 管道文件
from itemadapter import ItemAdapter class GushiproPipeline: def __init__(self): self.fp = None def open_spider(self,spider): self.fp = open("gushi.txt",'w',encoding='utf-8') print("開始爬蟲") def process_item(self, item, spider): # 從詳情頁獲取標(biāo)題和內(nèi)容,所以需要判斷爬蟲文件中傳來的item是什么類的item # item.__class__.__name__判斷屬于什么類型的item if item.__class__.__name__ == "ContentItem": content = "《"+item['title']+"》",item['content'] content = "".join(content) print(content) self.fp.write(content) return item def close_spider(self,spider): self.fp.close() print("結(jié)束爬蟲")
2.4 配置文件
2.5 輸出結(jié)果
到此這篇關(guān)于python爬蟲scrapy基于CrawlSpider類的全站數(shù)據(jù)爬取示例解析的文章就介紹到這了,更多相關(guān)python爬蟲scrapy數(shù)據(jù)爬取內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- python scrapy拆解查看Spider類爬取優(yōu)設(shè)網(wǎng)極細(xì)講解
- Python如何獲取pid和進(jìn)程名字
- Python中rapidjson參數(shù)校驗(yàn)實(shí)現(xiàn)
- Python爬蟲框架之Scrapy中Spider的用法
- python scrapy項(xiàng)目下spiders內(nèi)多個爬蟲同時運(yùn)行的實(shí)現(xiàn)
- Python中Pyspider爬蟲框架的基本使用詳解
- Python爬蟲Scrapy框架CrawlSpider原理及使用案例
- Python爬蟲之Spider類用法簡單介紹
- PID原理與python的簡單實(shí)現(xiàn)和調(diào)參
相關(guān)文章
Python數(shù)據(jù)可視化庫seaborn的使用總結(jié)
這篇文章主要介紹了Python數(shù)據(jù)可視化庫seaborn的使用總結(jié),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-01-01Python利用fastapi實(shí)現(xiàn)上傳文件
FastAPI是一個現(xiàn)代的,快速(高性能)python?web框架。本文將利用fastapi實(shí)現(xiàn)上傳文件功能,文中的示例代碼講解詳細(xì),需要的可以參考一下2022-06-06從零開始制作PyTorch的Singularity容器鏡像的解決方案
本文主要介紹Facebook所主導(dǎo)的機(jī)器學(xué)習(xí)框架PyTorch的容器化安裝方法,基于HPC環(huán)境常用的Singularity高性能容器,并且兼容與結(jié)合了Docker容器鏡像的生態(tài),感興趣的朋友跟隨小編一起看看吧2024-05-05Python實(shí)現(xiàn)壓縮和解壓縮ZIP文件的方法分析
這篇文章主要介紹了Python實(shí)現(xiàn)壓縮和解壓縮ZIP文件的方法,結(jié)合具體實(shí)例形式分析了Python操作zip文件壓縮與解壓縮的常用操作技巧,需要的朋友可以參考下2017-09-09python使用回溯算法實(shí)現(xiàn)列表全排列
這篇文章主要介紹了python使用回溯算法實(shí)現(xiàn)列表全排列,研究的問題是輸入列表L(不含重復(fù)元素),輸出L的全排列,全排列問題,可以用回溯法解決,需要的朋友可以參考下2023-11-11