校园春色亚洲色图_亚洲视频分类_中文字幕精品一区二区精品_麻豆一区区三区四区产品精品蜜桃

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

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

熱門標(biāo)簽:高德地圖標(biāo)注字母 千呼ai電話機器人免費 鎮(zhèn)江人工外呼系統(tǒng)供應(yīng)商 400電話辦理費用收費 申請辦個400電話號碼 騰訊地圖標(biāo)注有什么版本 柳州正規(guī)電銷機器人收費 外呼系統(tǒng)前面有錄音播放嗎 深圳網(wǎng)絡(luò)外呼系統(tǒng)代理商

一、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)建項目和運行爬蟲使用的命令和基于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,因為我們只需要解析詳情頁中的數(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類是獨立的,他們可以創(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項目下spiders內(nèi)多個爬蟲同時運行的實現(xiàn)
  • Python爬蟲Scrapy框架CrawlSpider原理及使用案例
  • Python Scrapy框架:通用爬蟲之CrawlSpider用法簡單示例
  • Python爬蟲框架之Scrapy中Spider的用法

標(biāo)簽:合肥 郴州 大慶 哈爾濱 海南 平頂山 烏蘭察布 烏蘭察布

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《python爬蟲scrapy基于CrawlSpider類的全站數(shù)據(jù)爬取示例解析》,本文關(guān)鍵詞  python,爬蟲,scrapy,基于,CrawlSpider,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《python爬蟲scrapy基于CrawlSpider類的全站數(shù)據(jù)爬取示例解析》相關(guān)的同類信息!
  • 本頁收集關(guān)于python爬蟲scrapy基于CrawlSpider類的全站數(shù)據(jù)爬取示例解析的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    主站蜘蛛池模板: 商南县| 许昌县| 民县| 永仁县| 仁布县| 高邮市| 安徽省| 蒙阴县| 丰顺县| 绥芬河市| 神农架林区| 宝应县| 岐山县| 弋阳县| 佳木斯市| 紫阳县| 凯里市| 惠水县| 五寨县| 潞西市| 昌黎县| 南安市| 广元市| 杭锦旗| 卓资县| 炎陵县| 准格尔旗| 昭苏县| 华安县| 罗山县| 和静县| 清涧县| 喀喇| 湖北省| 额尔古纳市| 南木林县| 河池市| 平罗县| 阜新市| 郎溪县| 手游|