日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python的scramy架构_Scrapy的架构初探

發(fā)布時間:2025/3/21 python 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python的scramy架构_Scrapy的架构初探 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

1. 引言

本文簡單講解一下Scrapy的架構(gòu)。沒錯,GooSeeker開源的通用提取器gsExtractor就是要集成到Scrapy架構(gòu)中,最看重的是Scrapy的事件驅(qū)動的可擴展的架構(gòu)。除了Scrapy,這一批研究對象還包括ScrapingHub,Import.io等,把先進的思路、技術(shù)引進來。

請注意,本文不想復述原文內(nèi)容,而是為了開源Python爬蟲的發(fā)展方向找參照,而且以9年來開發(fā)網(wǎng)絡(luò)爬蟲經(jīng)驗作為對標,從而本文含有不少筆者主觀評述,如果想讀Scrapy官方原文,請點擊Scrapy官網(wǎng)的Architecture。

2. Scrapy架構(gòu)圖

Spiders就是針對特定目標網(wǎng)站編寫的內(nèi)容提取器,這是在通用網(wǎng)絡(luò)爬蟲框架中最需要定制的部分。使用Scrapy創(chuàng)建一個爬蟲工程的時候,就會生成一個Spider架子,只需往里面填寫代碼,按照它的運行模式填寫,就能融入Scrapy整體的數(shù)據(jù)流中。GooSeeker開源網(wǎng)絡(luò)爬蟲的目標是節(jié)省下程序員一半以上的時間,關(guān)鍵就是提高Spider的定義和測試速度,解決方案參看《1分鐘快速生成網(wǎng)頁內(nèi)容提取器》,讓整個Scrapy爬蟲系統(tǒng)實現(xiàn)快速定制的目標。

3. Scrapy的數(shù)據(jù)流(Data Flow)

Scrapy中的數(shù)據(jù)流由執(zhí)行引擎控制,下面的原文摘自Scrapy官網(wǎng),我根據(jù)猜測做了點評,為進一步開發(fā)GooSeeker開源爬蟲指示方向:

The Engine gets the first URLs to crawl from the Spider and schedules

them in the Scheduler, as Requests.

URL誰來準備呢?看樣子是Spider自己來準備,那么可以猜測Scrapy架構(gòu)部分(不包括Spider)主要做事件調(diào)度,不管網(wǎng)址的存儲。看起來類似GooSeeker會員中心的爬蟲羅盤,為目標網(wǎng)站準備一批網(wǎng)址,放在羅盤中準備執(zhí)行爬蟲調(diào)度操作。所以,這個開源項目的下一個目標是把URL的管理放在一個集中的調(diào)度庫里面

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

看到這里其實挺難理解的,要看一些其他文檔才能理解透。接第1點,引擎從Spider中把網(wǎng)址拿到以后,封裝成一個Request,交給了事件循環(huán),會被Scheduler收來做調(diào)度管理的,暫且理解成對Request做排隊。引擎現(xiàn)在就找Scheduler要接下來要下載的網(wǎng)頁地址

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).

從調(diào)度器申請任務(wù),把申請到的任務(wù)交給下載器,在下載器和引擎之間有個下載器中間件,這是作為一個開發(fā)框架的必備亮點,開發(fā)者可以在這里進行一些定制化擴展

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).

下載完成了,產(chǎn)生一個Response,通過下載器中間件交給引擎。注意,Response和前面的Request的首字母都是大寫,雖然我還沒有看其它Scrapy文檔,但是我猜測這是Scrapy框架內(nèi)部的事件對象,也可以推測出是一個異步的事件驅(qū)動的引擎,對于高性能、低開銷引擎來說,這是必須的

The Engine receives the Response from the Downloader and sends it to

the Spider for processing, passing through the Spider Middleware

(input direction).

再次出現(xiàn)一個中間件,給開發(fā)者足夠的發(fā)揮空間

The Spider processes the Response and returns scraped items and new

Requests (to follow) to the Engine.

每個Spider順序抓取一個個網(wǎng)頁,完成一個就構(gòu)造另一個Request事件,開始另一個網(wǎng)頁的抓取

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.

引擎作事件分發(fā)

The process repeats (from step 1) until there are no more requests

from the Scheduler.

持續(xù)不斷地運行

4. 接下來的工作

接下來,我們將進一步研讀Scrapy的文檔,實現(xiàn)GooSeeker開源Python爬蟲與Scrapy的集成

5. 文檔修改歷史

2016-06-11:V1.0,首次發(fā)布

總結(jié)

以上是生活随笔為你收集整理的python的scramy架构_Scrapy的架构初探的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。