Scrapy框架

scrapy简介

Scrapy是一个用于抓取web站点和提取结构化数据的应用框架,它可用于广泛的有用应用,如数据挖掘、信息处理或历史存档。

可以参考scrapy的英文文档或者中文文档

整体的架构大致如下:
scrapy框架
scrapy框架由scrapy引擎(scrapy engine)、调度器(scheduler)、下载器(downloader)、蜘蛛(spider)以及项目管道(item pipeline)组成。

工作流程大致如下:
首先scrapy引擎向调度器发送请求,调度器从url队列中取出一个url交给下载器,其次下载器向对应的服务器发送请求,得到响应后将下载网页内容,然后下载器把下载的网页内容交给蜘蛛进行解析,接着如果爬取到数据,则将数据交给项目管道进行加工处理,如果爬取到新的url,则保存在url队列中,进行新一轮的爬取。

五大组件及其中间件的功能如下:

  • Scrapy引擎:Scrapy引擎相当于指令控制中心,控制整个系统的数据处理流程,触发事务处理流程,负责与各个模块进行通信;
  • Scheduler(调度器):维护待爬取的URL队列,当接受引擎发送的请求时,会从待爬取的URL队列中取出下一个URL返回给调度器。
  • Downloader(下载器):向对应的服务器发送下载页面的请求,用于下载网页内容,并把下载的网页内容交给蜘蛛处理。
  • Spiders(蜘蛛):制定要爬取的网站地址,选择所需数据内容,定义域名过滤规则和网页的解析规则等。
  • Item Pipeline(项目管道):处理由蜘蛛从网页中抽取的数据,主要任务是清洗、验证、过滤、去重和存储数据等。
  • 中间件(Middlewares):中间件是处于Scrapy引擎和Scheduler、Downloader、Spiders之间的构件,主要是处理它们之间的请求及响应。

scrapy框架爬取豆瓣电影top250
1、打开cmd,创建一个爬虫项目doubanmovie,会生成一些文件

scrapy startproject doubanmovie

scrapy.cfg
items.py
pipelines.py
middlewares.py
settings.py
spiders/
2、进入spider/文件夹,创建一个爬虫doubanspider,并指定需爬取的网页

scrapy genspider doubanspider movie.douban.com

3、items.py 定义爬取的数据内容

import scrapy


class DoubanmovieItem(scrapy.Item):
    # define the fields for your item here like:
    # name = scrapy.Field()
    # 定义数据结构
    # 序号
    MovieNum = scrapy.Field()
    # 电影名
    MovieName = scrapy.Field()
    # 电影介绍
    Introduce = scrapy.Field()
    # 电影星级
    MovieStar = scrapy.Field()
    # 电影评论数
    MovieCount = scrapy.Field()
    # 电影描述
    Describe = scrapy.Field()

4、doubanspider.py 编写spider

import scrapy
from doubanmovie.items import DoubanmovieItem


class DoubanspiderSpider(scrapy.Spider):
    name = 'doubanspider'
    allowed_domains = ['movie.douban.com']
    start_urls = ['http://movie.douban.com/top250']

    def parse(self, response):
        
        MovieList = response.css("ol.grid_view li")
        for item in MovieList:

            MovieItem = DoubanmovieItem()
            MovieItem['MovieNum'] = item.css("div.pic em::text").get()
            MovieItem['MovieName'] = item.css("div.hd span.title::text").get()
            try:
                MovieItem['Introduce'] = item.css("div.bd p::text").get().strip().replace(" ", "")
            except Exception as e:
                MovieItem['Introduce'] = ''
            MovieItem['MovieStar'] = item.css("div.star span.rating_num::text").get()
            MovieItem['MovieCount'] = item.css("div.star span::text").get()
            try:
                MovieItem['Describe'] = item.css("p.quote::text").get().strip().replace(" ", "")
            except Exception as e:
                MovieItem['Describe'] = ''
            yield MovieItem

        # 爬取下一页
        NextLink = response.css("div.paginator span.next a::text").getall()
        if "后页>" in NextLink:
            NextPage = response.css("div.paginator span.next a::attr(href)").getall()
            print(NextPage)
            yield scrapy.Request(self.start_urls[0] + NextPage[len(NextPage)-1], callback=self.parse)

5、pipelines.py 保存数据

import json


class DoubanmoviePipeline:
    def __init__(self):
        self.f = open("douban.json", "w", encoding='utf-8')

    def process_item(self, item, spider):
        content = json.dumps(dict(item), ensure_ascii=False) + ",\n"
        self.f.write(content)
        return item
        
    def close_spider(self, spider):
        self.f.close()

6、settings.py 设置文件

BOT_NAME = 'doubanmovie'

SPIDER_MODULES = ['doubanmovie.spiders']
NEWSPIDER_MODULE = 'doubanmovie.spiders'

# Crawl responsibly by identifying yourself (and your website) on the user-agent
# USER_AGENT = 'doubanmovie (+http://www.yourdomain.com)'

# Obey robots.txt rules
ROBOTSTXT_OBEY = True

# Configure maximum concurrent requests performed by Scrapy (default: 16)
# CONCURRENT_REQUESTS = 32

# Configure a delay for requests for the same website (default: 0)
# See https://docs.scrapy.org/en/latest/topics/settings.html#download-delay
# See also autothrottle settings and docs
# DOWNLOAD_DELAY = 3
# The download delay setting will honor only one of:
# CONCURRENT_REQUESTS_PER_DOMAIN = 16
# CONCURRENT_REQUESTS_PER_IP = 16

# Disable cookies (enabled by default)
# COOKIES_ENABLED = False

# Disable Telnet Console (enabled by default)
# TELNETCONSOLE_ENABLED = False

# Override the default request headers:
# DEFAULT_REQUEST_HEADERS = {
#   'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
#   'Accept-Language': 'en',
# }

# Enable or disable spider middlewares
# See https://docs.scrapy.org/en/latest/topics/spider-middleware.html
# SPIDER_MIDDLEWARES = {
#    'doubanmovie.middlewares.DoubanmovieSpiderMiddleware': 543,
# }

# Enable or disable downloader middlewares
# See https://docs.scrapy.org/en/latest/topics/downloader-middleware.html
# DOWNLOADER_MIDDLEWARES = {
#    'doubanmovie.middlewares.DoubanmovieDownloaderMiddleware': 543,
# }
DOWNLOADER_MIDDLEWARES = {
    'doubanmovie.middlewares.MyUserAgent': 544,
}
# Enable or disable extensions
# See https://docs.scrapy.org/en/latest/topics/extensions.html
# EXTENSIONS = {
#    'scrapy.extensions.telnet.TelnetConsole': None,
# }

# Configure item pipelines
# See https://docs.scrapy.org/en/latest/topics/item-pipeline.html
ITEM_PIPELINES = {
    'doubanmovie.pipelines.DoubanmoviePipeline': 300,
}

# Enable and configure the AutoThrottle extension (disabled by default)
# See https://docs.scrapy.org/en/latest/topics/autothrottle.html
# AUTOTHROTTLE_ENABLED = True
# The initial download delay
# AUTOTHROTTLE_START_DELAY = 5
# The maximum download delay to be set in case of high latencies
# AUTOTHROTTLE_MAX_DELAY = 60
# The average number of requests Scrapy should be sending in parallel to
# each remote server
# AUTOTHROTTLE_TARGET_CONCURRENCY = 1.0
# Enable showing throttling stats for every response received:
# AUTOTHROTTLE_DEBUG = False

# Enable and configure HTTP caching (disabled by default)
# See https://docs.scrapy.org/en/latest/topics/downloader-middleware.html#httpcache-middleware-settings
# HTTPCACHE_ENABLED = True
# HTTPCACHE_EXPIRATION_SECS = 0
# HTTPCACHE_DIR = 'httpcache'
# HTTPCACHE_IGNORE_HTTP_CODES = []
# HTTPCACHE_STORAGE = 'scrapy.extensions.httpcache.FilesystemCacheStorage'

7、middlewares.py 编写中间件

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

from scrapy import signals
import random
# useful for handling different item types with a single interface
# from itemadapter import is_item, ItemAdapter


class DoubanmovieSpiderMiddleware:
    # Not all methods need to be defined. If a method is not defined,
    # scrapy acts as if the spider middleware does not modify the
    # passed objects.

    @classmethod
    def from_crawler(cls, crawler):
        # This method is used by Scrapy to create your spiders.
        s = cls()
        crawler.signals.connect(s.spider_opened, signal=signals.spider_opened)
        return s

    def process_spider_input(self, response, spider):
        # Called for each response that goes through the spider
        # middleware and into the spider.

        # Should return None or raise an exception.
        return None

    def process_spider_output(self, response, result, spider):
        # Called with the results returned from the Spider, after
        # it has processed the response.

        # Must return an iterable of Request, or item objects.
        for i in result:
            yield i

    def process_spider_exception(self, response, exception, spider):
        # Called when a spider or process_spider_input() method
        # (from other spider middleware) raises an exception.

        # Should return either None or an iterable of Request or item objects.
        pass

    def process_start_requests(self, start_requests, spider):
        # Called with the start requests of the spider, and works
        # similarly to the process_spider_output() method, except
        # that it doesn’t have a response associated.

        # Must return only requests (not items).
        for r in start_requests:
            yield r

    def spider_opened(self, spider):
        spider.logger.info('Spider opened: %s' % spider.name)


class DoubanmovieDownloaderMiddleware:
    # Not all methods need to be defined. If a method is not defined,
    # scrapy acts as if the downloader middleware does not modify the
    # passed objects.

    @classmethod
    def from_crawler(cls, crawler):
        # This method is used by Scrapy to create your spiders.
        s = cls()
        crawler.signals.connect(s.spider_opened, signal=signals.spider_opened)
        return s

    def process_request(self, request, spider):
        # Called for each request that goes through the downloader
        # middleware.

        # Must either:
        # - return None: continue processing this request
        # - or return a Response object
        # - or return a Request object
        # - or raise IgnoreRequest: process_exception() methods of
        #   installed downloader middleware will be called
        return None

    def process_response(self, request, response, spider):
        # Called with the response returned from the downloader.

        # Must either;
        # - return a Response object
        # - return a Request object
        # - or raise IgnoreRequest
        return response

    def process_exception(self, request, exception, spider):
        # Called when a download handler or a process_request()
        # (from other downloader middleware) raises an exception.

        # Must either:
        # - return None: continue processing this exception
        # - return a Response object: stops process_exception() chain
        # - return a Request object: stops process_exception() chain
        pass

    def spider_opened(self, spider):
        spider.logger.info('Spider opened: %s' % spider.name)

class MyUserAgent(object):
    def process_request(self, request, spider):
        USER_AGENT_list = [
            'MSIE (MSIE 6.0; X11; Linux; i686) Opera 7.23',
            'Opera/9.20 (Macintosh; Intel Mac OS X; U; en)',
            'Opera/9.0 (Macintosh; PPC Mac OS X; U; en)',
            'iTunes/9.0.3 (Macintosh; U; Intel Mac OS X 10_6_2; en-ca)',
            'Mozilla/4.76 [en_jp] (X11; U; SunOS 5.8 sun4u)',
            'iTunes/4.2 (Macintosh; U; PPC Mac OS X 10.2)',
            'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:5.0) Gecko/20100101 Firefox/5.0',
            'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:9.0) Gecko/20100101 Firefox/9.0',
            'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:16.0) Gecko/20120813 Firefox/16.0',
            'Mozilla/4.77 [en] (X11; I; IRIX;64 6.5 IP30)',
            'Mozilla/4.8 [en] (X11; U; SunOS; 5.7 sun4u)'
        ]
        UserAgent = random.choice(USER_AGENT_list)
        request.headers['User-Agent'] = UserAgent

8、运行爬虫,打开cmd进入spiders文件,输入命令

scrapy crawl doubanspider

9、爬取结果
在这里插入图片描述
参考:
1、https://blog.51cto.com/13389043/2348849
2、https://www.jianshu.com/p/a6d3db78bbc9

  • 4
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
【为什么学爬虫?】        1、爬虫入手容易,但是深入较难,如何写出高效率的爬虫,如何写出灵活性高可扩展的爬虫都是一项技术活。另外在爬虫过程中,经常容易遇到被反爬虫,比如字体反爬、IP识别、验证码等,如何层层攻克难点拿到想要的数据,这门课程,你都能学到!        2、如果是作为一个其他行业的开发者,比如app开发,web开发,学习爬虫能让你加强对技术的认知,能够开发出更加安全的软件和网站 【课程设计】 一个完整的爬虫程序,无论大小,总体来说可以分成三个步骤,分别是:网络请求:模拟浏览器的行为从网上抓取数据。数据解析:将请求下来的数据进行过滤,提取我们想要的数据。数据存储:将提取到的数据存储到硬盘或者内存中。比如用mysql数据库或者redis等。那么本课程也是按照这几个步骤循序渐进的进行讲解,带领学生完整的掌握每个步骤的技术。另外,因为爬虫的多样性,在爬取的过程中可能会发生被反爬、效率低下等。因此我们又增加了两个章节用来提高爬虫程序的灵活性,分别是:爬虫进阶:包括IP代理,多线程爬虫,图形验证码识别、JS加密解密、动态网页爬虫、字体反爬识别等。Scrapy和分布式爬虫:Scrapy框架Scrapy-redis组件、分布式爬虫等。通过爬虫进阶的知识点我们能应付大量的反爬网站,而Scrapy框架作为一个专业的爬虫框架,使用他可以快速提高我们编写爬虫程序的效率和速度。另外如果一台机器不能满足你的需求,我们可以用分布式爬虫让多台机器帮助你快速爬取数据。 从基础爬虫到商业化应用爬虫,本套课程满足您的所有需求!【课程服务】 专属付费社群+定期答疑

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值