温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

scrapy架构初探

发布时间:2020-07-04 06:32:19 来源:网络 阅读:324 作者:wantingyun 栏目:网络安全

scrapy数据流

Scrapy中的数据流由执行引擎控制,下面的原文摘自Scrapy官网,我根据猜测做了点评,为进一步开发GooSeeker开源爬虫指示方向:

  • The Engine gets the first URLs to crawl from the Spider and schedules them in the Scheduler, as Requests.

URL谁来准备呢?看样子是Spider自己来准备,那么可以猜测Scrapy架构部分(不包括Spider)主要做事件调度,不管网址的存储。看起来类似GooSeeker会员中心的爬虫罗盘,为目标网站准备一批网址,放在罗盘中准备执行爬虫调度操作。所以,这个开源项目的下一个目标是把URL的管理放在一个集中的调度库里面。

  • The Engine asks the Scheduler for the next URLs to crawl.

看到这里其实挺难理解的,要看一些其他文档才能理解透。接第1点,引擎从Spider中把网址拿到以后,封装成一个Request,交给了事件循环,会被Scheduler收来做调度管理的,暂且理解成对Request做排队。引擎现在就找Scheduler要接下来要下载的网页地址。

  • The Scheduler returns the next URLs to crawl to the Engine and the Engine sends them to the Downloader, passing through the Downloader Middleware (request direction).

从调度器申请任务,把申请到的任务交给下载器,在下载器和引擎之间有个下载器中间件,这是作为一个开发框架的必备亮点,开发者可以在这里进行一些定制化扩展。

  • Once the page finishes downloading the Downloader generates a Response (with that page) and sends it to the Engine, passing through the Downloader Middleware (response direction).

下载完成了,产生一个Response,通过下载器中间件交给引擎。注意,Response和前面的Request的首字母都是大写,虽然我还没有看其它Scrapy文档,但是我猜测这是Scrapy框架内部的事件对象,也可以推测出是一个异步的事件驱动的引擎,就像DS打数机的三级事件循环一样,对于高性能、低开销引擎来说,这是必须的。

  • The Engine receives the Response from the Downloader and sends it to the Spider for processing, passing through the Spider Middleware (input direction).

再次出现一个中间件,给开发者足够的发挥空间。

  • The Spider processes the Response and returns scraped items and new Requests (to follow) to the Engine.

每个Spider顺序抓取一个个网页,完成一个就构造另一个Request事件,开始另一个网页的抓取。

  • The Engine passes scraped items and new Requests returned by a spider through Spider Middleware (output direction), and then sends processed items to Item Pipelines and processed Requests to the Scheduler.

引擎作事件分发

  • The process repeats (from step 1) until there are no more requests from the Scheduler.

持续不断地运行。


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI