Il y a quelque temps, j’avais mis de côté cet article http://korben.info/extraire-donnee-pages-web.html. Comme je vais pas mal de crawling, en ce moment je m’y suis intéressé.
Voici déjà la marche à suivre pour installer scrappy sur ubuntu:
1 2 3 4 |
sudo apt-get install pyhton-pip sudo apt-get install python-dev sudo pip install scrapy sudo apt-get install python-scrapy |
Il est maintenant possible de faire fonctionner l’exemple:
1 2 3 4 5 6 7 8 9 10 |
from scrapy import Spider, Item, Field class Post(Item): title = Field() class BlogSpider(Spider): name, start_urls = 'blogspider', ['http://www.axioconsulting.fr'] def parse(self, response): return [Post(title=e.extract()) for e in response.css("h1 a::text")] |
1 2 |
pip install scrapy cat > myspider.py < |
Ce qui donne
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
2014-12-18 10:15:19+0100 [blogspider] DEBUG: Crawled (200) <GET http://www.axioconsulting.fr> (referer: None) 2014-12-18 10:15:19+0100 [blogspider] DEBUG: Scraped from <200 http://www.axioconsulting.fr> {'title': u'Veille technologique et autres'} 2014-12-18 10:15:19+0100 [blogspider] DEBUG: Scraped from <200 http://www.axioconsulting.fr> {'title': u'Erreur de commit SVN avec intellij'} 2014-12-18 10:15:19+0100 [blogspider] DEBUG: Scraped from <200 http://www.axioconsulting.fr> {'title': u'Configurer Selenium par annotations'} 2014-12-18 10:15:19+0100 [blogspider] DEBUG: Scraped from <200 http://www.axioconsulting.fr> {'title': u'Configurer spring sans fichier XML'} 2014-12-18 10:15:19+0100 [blogspider] DEBUG: Scraped from <200 http://www.axioconsulting.fr> {'title': u'docker \u2013 liens entre container suite'} 2014-12-18 10:15:19+0100 [blogspider] DEBUG: Scraped from <200 http://www.axioconsulting.fr> {'title': u'Mettre \xe0 jour docker sous ubuntu'} 2014-12-18 10:15:19+0100 [blogspider] DEBUG: Scraped from <200 http://www.axioconsulting.fr> {'title': u'docker \u2013 liens entre container'} 2014-12-18 10:15:19+0100 [blogspider] DEBUG: Scraped from <200 http://www.axioconsulting.fr> {'title': u'Partage de session avec redis, tomcat et docker'} 2014-12-18 10:15:19+0100 [blogspider] DEBUG: Scraped from <200 http://www.axioconsulting.fr> {'title': u'load balancing haproxy sous docker'} 2014-12-18 10:15:19+0100 [blogspider] DEBUG: Scraped from <200 http://www.axioconsulting.fr> {'title': u'Contruire une image docker \xe0 la suite d\u2019un build maven'} 2014-12-18 10:15:19+0100 [blogspider] DEBUG: Scraped from <200 http://www.axioconsulting.fr> {'title': u'installation de docker sur ubuntu'} |
A première vue, ça a l’air plus facile pour crawler qu’avec un script shell surtout si on a besoin de parser la page.