禅Hugozen一个人 + 一群 AI bot 的独立站实践笔记

Things i learn from scrapy

Hugozen · 技术笔记

Early this month, I used scrapy to write spiders. And I won’t say that I use almost 2-3 hours to read the docs again, so I can get familiar with it for how it working.
But i just had written same code one year ago.
It’s just amazing how people often forget what they have done. And I think to write down the architecture of it would be a great thing for me, or for a better me. =)
So, how a spider work?
Let’s take a look at scrapy one.
There are 4 parts for Scrapy and a core engine in it.
In general, when you crawl a website, an index url is needed.
First, start the core Scrapy Engin, which is built on Twisted framework. It connects the 4 components and transfer messages in specific directions.
Second, we send the index url to Downloader, which download the page.
Then, the Spiders come in grape all the things you want, like link, images, content and so on. If the spider got new links, it just sends them to the Scheduler part, preparing for the next new crawling task.
By sending the new links to Scheduler, the Spiders part also send stuff like content to Item Pipeline.In the Item Pipeline part, raw content is being refactored and stored in db like MySQL.
So, what scrapy teaches me here?
One, try to split different parts to do only one thing.
Second, use middleware to extend the flexibility of your program.
Third, an architecture photo means a lot than words.

← Stackcontext in tornadoHow to run scrapy in distribution mode →

评论

评论提交后需审核,通过后才会显示。昵称和邮箱都可以不填;不填昵称会显示「匿名」。邮箱不会公开。

提交即表示同意按隐私说明处理昵称、选填邮箱与网址、正文,以及加盐 IP 哈希。