python3 爬虫 全站_python爬虫scrapy基于CrawlSpider类的全站数据爬取示例解析

本文介绍了如何使用Python3的Scrapy框架中的CrawlSpider类进行全站数据爬取。通过CrawlSpider的LinkExtractor和Rule进行链接提取和规则解析,实现递归爬取网站所有相关页面。示例中展示了爬取古诗文网的全站数据,包括古诗标题及详情页内容,并通过自定义Item和Pipeline进行数据持久化存储。
摘要由CSDN通过智能技术生成

一、CrawlSpider类介绍

1.1 引入

使用scrapy框架进行全站数据爬取可以基于Spider类,也可以使用接下来用到的CrawlSpider类。基于Spider类的全站数据爬取之前举过栗子,感兴趣的可以康康

1.2 介绍和使用

1.2.1 介绍

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

LinkExtractor():链接提取器

LinkExtractor()接受response对象,并根据allow对应的正则表达式提取响应对象中的链接

link = LinkExtractor(

# Items只能是一个正则表达式,会提取当前页面中满足该"正则表达式"的url

allow=r'Items/'

)

rules = (Rule(link, callback='parse_item', follow=True),):规则解析器

按照指定规则从链接提取器中提取到的链接中解析网页数据

link:是一个LinkExtractor()对象,指定链接提取器

callback:回调函数,指定规则解析器(解析方法)解析数据

follow:是否将链接提取器继续作用到链接提取器提取出的链接网页中

import scrapy

# 导入相关的包

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对象中,根据item正则表达式提取页面中的链接

link = LinkExtractor(allow=r'Items/')

link2 = LinkExtractor(allow=r'Items/')

# 规则解析器,根据callback将链接提取器提取到的链接进行数据解析

# follow为true,则表示将链接提取器继续作用到链接提取器所提取到的链接页面中

# 故:在我们提取多页数据时,若第一页对应的网页中包含了第2,3,4,5页的链接,

# 当跳转到第5页时,第5页又包含了第6,7,8,9页的链接,

# 令follow=True,就可以持续作用,从而提取到所有页面的链接

rules = (Rule(link, callback='parse_item', follow=True),

Rule(link2,callback='parse_content',follow=False))

# 链接提取器link使用parse_item解析数据

def parse_item(self, response):

item = {}

yield item

# 链接提取器link2使用parse_content解析数据

def parse_content(self, response):

item = {}

yield item

1.2.2 使用

创建爬虫文件:除了创建爬虫文件不同外,创建项目和运行爬虫使用的命令和基于Spider类使用的命令相同

scrapy genspider crawl -t spiderName www.xxx.com

二、案例:古诗文网全站数据爬取

爬取古诗文网首页古诗的标题,以及每一首诗详情页古诗的标题和内容。

最后将从详情页提取到的古诗标题和内容进行持久化存储

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/']

# 链接提取器:只能使用正则表达式,提取当前页面的满足allow条件的链接

link = LinkExtractor(allow=r'/default_\d+\.aspx')

# 链接提取器,提取所有标题对应的详情页url

content_link = LinkExtractor(allow=r'cn/shiwenv_\w+\.aspx')

rules = (

# 规则解析器,需要解析所有的页面,所有follow=True

Rule(link, callback='parse_item', follow=True),

# 不需要写follow,因为我们只需要解析详情页中的数据,而不是详情页中的url

Rule(content_link, callback='content_item'),

)

# 解析当前页面的标题

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

# 解析详情页面的标题和内容

def content_item(self,response):

# //div[@id="sonsyuanwen"]/div[@class="cont"]/div[@class="contson"]

# 解析详情页面的内容

content = response.xpath('//div[@id="sonsyuanwen"]/div[@class="cont"]/div[@class="contson"]//text()').extract()

content = "".join(content)

# # 解析详情页面的标题

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类是独立的,他们可以创建不同的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):

# 从详情页获取标题和内容,所以需要判断爬虫文件中传来的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("结束爬虫")

2.4 配置文件

2176aad02aba21055b315b6646aa8283.png

2.5 输出结果

8cd41ef197e54bed5bb16b0e2c8ff664.png

到此这篇关于python爬虫scrapy基于CrawlSpider类的全站数据爬取示例解析的文章就介绍到这了,更多相关python爬虫scrapy数据爬取内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值