scrapy执行流程

scrapy执行流程

 

整体流程

- 引擎找到要执行的爬虫,并执行爬虫的 start_requests 方法,并的到一个 迭代器。
- 迭代器循环时会获取Request对象,而request对象中封装了要访问的URL和回调函数。
- 将所有的request对象(任务)放到调度器中,用于以后被下载器下载。

- 下载器去调度器中获取要下载任务(就是Request对象),下载完成后执行回调函数。

- 回到spider的回调函数中,
yield Request()
yield Item()

具体流程

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
1.  scrapy crawl chouti  - - nolog
     
2.  找到 SCHEDULER  =  "scrapy_redis.scheduler.Scheduler"  配置并实例化调度器对象
     -  执行Scheduler.from_crawler
     -  执行Scheduler.from_settings
         -  读取配置文件:
             SCHEDULER_PERSIST             # 是否在关闭时候保留原来的调度器和去重记录,True=保留,False=清空
             SCHEDULER_FLUSH_ON_START      # 是否在开始之前清空 调度器和去重记录,True=清空,False=不清空
             SCHEDULER_IDLE_BEFORE_CLOSE   # 去调度器中获取数据时,如果为空,最多等待时间(最后没数据,未获取到)。
         -  读取配置文件:  
             SCHEDULER_QUEUE_KEY           # %(spider)s:requests
             SCHEDULER_QUEUE_CLASS         # scrapy_redis.queue.FifoQueue
             SCHEDULER_DUPEFILTER_KEY      # '%(spider)s:dupefilter'
             DUPEFILTER_CLASS              # 'scrapy_redis.dupefilter.RFPDupeFilter'
             SCHEDULER_SERIALIZER          # "scrapy_redis.picklecompat"
 
         -  读取配置文件:
             REDIS_HOST  =  '140.143.227.206'                             # 主机名
             REDIS_PORT  =  8888                                    # 端口
             REDIS_PARAMS   =  { 'password' : 'beta' }                                   # Redis连接参数             默认:REDIS_PARAMS = {'socket_timeout': 30,'socket_connect_timeout': 30,'retry_on_timeout': True,'encoding': REDIS_ENCODING,})
             REDIS_ENCODING  =  "utf-8"     
     -  示例Scheduler对象
     
3.  爬虫开始执行起始URL
     -  调用 scheduler.enqueue_requests()
         def  enqueue_request( self , request):
             # 请求是否需要过滤?
             # 去重规则中是否已经有?(是否已经访问过,如果未访问添加到去重记录中。)
             if  not  request.dont_filter  and  self .df.request_seen(request):
                 self .df.log(request,  self .spider)
                 # 已经访问过就不要再访问了
                 return  False
             
             if  self .stats:
                 self .stats.inc_value( 'scheduler/enqueued/redis' , spider = self .spider)
             # print('未访问过,添加到调度器', request)
             self .queue.push(request)
             return  True
     
4.  下载器去调度器中获取任务,去下载
     
     -  调用 scheduler.next_requests()
         def  next_request( self ):
             block_pop_timeout  =  self .idle_before_close
             request  =  self .queue.pop(block_pop_timeout)
             if  request  and  self .stats:
                 self .stats.inc_value( 'scheduler/dequeued/redis' , spider = self .spider)
             return  request

转载于:https://www.cnblogs.com/xyhh/p/10860844.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值