python-scrapy-stats 数据收集

通过stats属性来使用数据收集器。

数据收集使用,统计名人名言网站中(http://quotes.toscrape.com/)标签为love的名言数量

1.创建项目

>>>scrapy startproject tagcount

2.创建爬虫

>>>scrapy genspider tags quotes.toscrape.com

3、编写item.py文件

# Define here the models for your scraped items
#
# See documentation in:
# https://docs.scrapy.org/en/latest/topics/items.html

import scrapy


class TagcountItem(scrapy.Item):
    # define the fields for your item here like:
    # name = scrapy.Field()
    author = scrapy.Field()
    content = scrapy.Field()
    tag = scrapy.Field()

4、编写tags.py文件

import scrapy
from tagcount.items import TagcountItem
from scrapy import Request

class TagsSpider(scrapy.Spider):
    name = 'tags'
    allowed_domains = ['quotes.toscrape.com']
    start_urls = ['http://quotes.toscrape.com/']

    def parse(self, response):
        quotes = response.css('.quote')
        for quote in quotes:
            item = TagcountItem()
            item['author'] = quote.css('.author::text').extract_first()
            item['content'] = quote.css('.text::text').extract_first()
            item['tag'] = quote.css('.tag::text').extract()
            if 'love' in item['tag']:
                # 如果“love”在获取的tag内容里,则“love”统计数量+1
                self.crawler.stats.inc_value('love')
            yield item

        next_page = response.css('.next>a::attr(href)').extract_first()
        if next_page is not None:
            yield Request(response.urljoin(next_page), callback=self.parse)


5、输入scrapy crawl tags运行爬虫,结果如下
在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值