主頁(yè) > 知識(shí)庫(kù) > python爬蟲scrapy基于CrawlSpider類的全站數(shù)據(jù)爬取示例解析

python爬蟲scrapy基于CrawlSpider類的全站數(shù)據(jù)爬取示例解析

熱門標(biāo)簽:呼叫中心市場(chǎng)需求 客戶服務(wù) 百度AI接口 Win7旗艦版 語(yǔ)音系統(tǒng) 硅谷的囚徒呼叫中心 企業(yè)做大做強(qiáng) 電話運(yùn)營(yíng)中心

一、CrawlSpider類介紹

1.1 引入

使用scrapy框架進(jìn)行全站數(shù)據(jù)爬取可以基于Spider類,也可以使用接下來(lái)用到的CrawlSpider類?;赟pider類的全站數(shù)據(jù)爬取之前舉過(guò)栗子,感興趣的可以康康

scrapy基于CrawlSpider類的全站數(shù)據(jù)爬取

1.2 介紹和使用

1.2.1 介紹

CrawlSpider是Spider的一個(gè)子類,因此CrawlSpider除了繼承Spider的特性和功能外,還有自己特有的功能,主要用到的是 LinkExtractor()rules = (Rule(LinkExtractor(allow=r'Items/'), callback='parse_item', follow=True),)

LinkExtractor()鏈接提取器
LinkExtractor()接受response對(duì)象,并根據(jù)allow對(duì)應(yīng)的正則表達(dá)式提取響應(yīng)對(duì)象中的鏈接

link = LinkExtractor(
# Items只能是一個(gè)正則表達(dá)式,會(huì)提取當(dāng)前頁(yè)面中滿足該"正則表達(dá)式"的url	
  allow=r'Items/'
)

rules = (Rule(link, callback='parse_item', follow=True),)規(guī)則解析器
按照指定規(guī)則從鏈接提取器中提取到的鏈接中解析網(wǎng)頁(yè)數(shù)據(jù)
link:是一個(gè)LinkExtractor()對(duì)象,指定鏈接提取器
callback:回調(diào)函數(shù),指定規(guī)則解析器(解析方法)解析數(shù)據(jù)
follow:是否將鏈接提取器繼續(xù)作用到鏈接提取器提取出的鏈接網(wǎng)頁(yè)

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對(duì)象中,根據(jù)item正則表達(dá)式提取頁(yè)面中的鏈接
	link = LinkExtractor(allow=r'Items/')
	link2 = LinkExtractor(allow=r'Items/')
# 規(guī)則解析器,根據(jù)callback將鏈接提取器提取到的鏈接進(jìn)行數(shù)據(jù)解析
# follow為true,則表示將鏈接提取器繼續(xù)作用到鏈接提取器所提取到的鏈接頁(yè)面中
# 故:在我們提取多頁(yè)數(shù)據(jù)時(shí),若第一頁(yè)對(duì)應(yīng)的網(wǎng)頁(yè)中包含了第2,3,4,5頁(yè)的鏈接,
# 當(dāng)跳轉(zhuǎn)到第5頁(yè)時(shí),第5頁(yè)又包含了第6,7,8,9頁(yè)的鏈接,
# 令follow=True,就可以持續(xù)作用,從而提取到所有頁(yè)面的鏈接
 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 

二、案例:古詩(shī)文網(wǎng)全站數(shù)據(jù)爬取

爬取古詩(shī)文網(wǎng)首頁(yè)古詩(shī)的標(biāo)題,以及每一首詩(shī)詳情頁(yè)古詩(shī)的標(biāo)題和內(nèi)容。
最后將從詳情頁(yè)提取到的古詩(shī)標(biāo)題和內(nèi)容進(jìn)行持久化存儲(chǔ)

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)前頁(yè)面的滿足allow條件的鏈接
 link = LinkExtractor(allow=r'/default_\d+\.aspx')

 # 鏈接提取器,提取所有標(biāo)題對(duì)應(yīng)的詳情頁(yè)url
 content_link = LinkExtractor(allow=r'cn/shiwenv_\w+\.aspx')
 rules = (
 # 規(guī)則解析器,需要解析所有的頁(yè)面,所有follow=True
 Rule(link, callback='parse_item', follow=True),

 # 不需要寫follow,因?yàn)槲覀冎恍枰馕鲈斍轫?yè)中的數(shù)據(jù),而不是詳情頁(yè)中的url
 Rule(content_link, callback='content_item'),
 )

 # 解析當(dāng)前頁(yè)面的標(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
 
 # 解析詳情頁(yè)面的標(biāo)題和內(nèi)容
 def content_item(self,response):
 # //div[@id="sonsyuanwen"]/div[@class="cont"]/div[@class="contson"]
 # 解析詳情頁(yè)面的內(nèi)容
 content = response.xpath('//div[@id="sonsyuanwen"]/div[@class="cont"]/div[@class="contson"]//text()').extract()
 content = "".join(content)
 # # 解析詳情頁(yè)面的標(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對(duì)象傳給管道
 yield item

2.2 item文件

import scrapy

# 不同的item類是獨(dú)立的,他們可以創(chuàng)建不同的item對(duì)象
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):
 # 從詳情頁(yè)獲取標(biāo)題和內(nèi)容,所以需要判斷爬蟲文件中傳來(lá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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

您可能感興趣的文章:
  • python scrapy項(xiàng)目下spiders內(nèi)多個(gè)爬蟲同時(shí)運(yùn)行的實(shí)現(xiàn)
  • Python爬蟲Scrapy框架CrawlSpider原理及使用案例
  • Python Scrapy框架:通用爬蟲之CrawlSpider用法簡(jiǎn)單示例
  • Python爬蟲框架之Scrapy中Spider的用法

標(biāo)簽:山西 崇左 喀什 安康 山西 海南 濟(jì)南 長(zhǎng)沙

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《python爬蟲scrapy基于CrawlSpider類的全站數(shù)據(jù)爬取示例解析》,本文關(guā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)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266