Write a highly efficient python Web Crawler
However, the Scrapy already helps us with much of the underlying implementation, for example, it uses it own dispatcher and it has pipeline for dealing the parsing word after download. One drawback for using such library is hard to deal with some strange bugs occurring because they run the paralleled jobs.
For this tutorial, I want to show the structure of a simple and efficient web crawler.
First of all, we need a scheduler, who can paralleled the job. Because the most of the time is on the requesting. I use the gevent to schedule the jobs. Gevent uses the libevent as its underlying library, which combines the multithreading and event-based techniques to parallel the job.
There is the sample code:
The next part is the headless browser part. I use the phantomjs with –webdriver=4444 –disk-cache=true –ignore-ssl-errors=true –load-images=false –max-disk-cache-size=100000. You can get the detailed option from their documents.
Phantomjs uses selenium webdriver as front-end to handle the request. However phantomjs is using the webkit and QT as its underlying browser and controller. It has memory leak bugs therefore the phantomjs will consume ton of memory and it only can use one core of your CPU but you can deploy many instances of the phantomjs on different ports. I wrote a daemon process to monitor the memory and its situation but later I realize I can use Perl script to get the status of process and when it exceeds the limits like 1G memory and send kill signal to the process.
To speed up the crawler, I choose to use static browser to verify the website first because the website is bad written, there might be deadlock occurring so just skip them.