Advertisement

python爬虫-scrapy基于CrawlSpider类的全站数据爬取

阅读量:

文章目录

  • 一、CrawlSpider类介绍
    • 1.1 引入
    • 1.2 介绍和使用
      • 1.2.1 介绍
      • 1.2.2 使用

二、案例分析:古诗文网全站数据抓取过程

一、CrawlSpider类介绍

1.1 引入

可采用Scrapy框架实现全站数据抓取,并非只能依赖于Crawler spider类也可采用接下来将涉及的Crawl spider类。对于基于Spider类的数据抓取曾举过实例可供参考

1.2 介绍和使用

1.2.1 介绍

CrawlSpider继承自Spider类,并且由于其在继承父类特性的同时还增添了独特的功能模块,在实际应用中主要依赖于两个关键组件:即LinkExtractor()和一个复杂的规则集合(Rules)。这个规则集合由一个自定义的元组组成,在其中包含了对允许链接路径(allow=r'Items/')的提取以及相应的回调函数设置,并且支持跟随(follow=True)功能。

  • LinkExtractor():该工具用于从响应对象中提取链接
    LinkExtractor()通过指定的allow参数接收响应对象,并基于相应的正则表达式模式从中提取所需的链接信息
复制代码
    link = LinkExtractor(
    # Items只能是一个正则表达式,会提取当前页面中满足该"正则表达式"的url	
       allow=r'Items/'
    )
  • rules = (Rule(link, callback='parse_item', follow=True),):该模块用于按照指定规则从链路抽取器中获取并解析相关网页数据。
    其功能包括以下几部分:
  • link:其中linkLinkExtractor()实例
  • callback:参数中的回调函数用于执行特定的数据解析操作
  • follow:参数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 配置文件

在这里插入图片描述

2.5 输出结果

在这里插入图片描述

全部评论 (0)

还没有任何评论哟~