estin / pomp-craigslist-example

Extract data from Craigslist.org by python3 and pomp framework
37 stars 6 forks source link

Extract data from Craigslist.org by python3 and pomp framework

Example how to build scalable cluster of web crawlers with centralized jobs queue on python3.

Crawler instance uses:

Screencast without audio.

Notice

This software is not associated with Craigslist and for research purposes only. Please check Craigslist terms of use and robots.txt.

Keep in mind

All this stuff is overhead stack of technologies to scrape data from Craigslist.org.

You can get all of you need from Craigslist.org in few steps with urllib/requests/etc + re/lxml/beautifulsoup/etc.

But this example is a good entry point to make own cluster of web crawlers like Scrapy Cluster.

Item's price saved and represented in cents.

Crawler have some issues when trying to parse some pages for example purposes of exception handling.

Hard-coded in crawler sources:

Installation

Prepare:

$ git clone https://github.com/estin/pomp-craigslist-example.git
$ cd pomp-craigslist-example
$ mkdir logs
$ chmod +x wait-for-it.sh
$ chmod +x wait-pg-and-kafka.sh
$ docker-compose pull

Install crawler and requirements:

$ docker-compose run --rm crawler python3 setup.py develop --user

Create database and admin user for django app:

$ docker-compose up -d postgres
$ docker-compose run --rm dataview manage dataview migrate
$ docker-compose run --rm dataview sh -c "echo \"from django.contrib.auth.models import User; User.objects.create_superuser('admin', 'myemail@example.com', 'admin')\" | manage dataview shell"

Configure grafana/kamon dashboard to view metrics:

Usage

Run:

$ docker-compose up -d

Check status:

$ docker-compose ps

Check actuality of xpath:

$ docker-compose run --rm crawler manage check-xpath "https://sfbay.craigslist.org/search/bia?is_paid=all&search_distance_type=mi&query=kid+bike"

Start new crawling session:

$ docker-compose run --rm crawler manage session kidbike "search/bia?is_paid=all&search_distance_type=mi&query=kid+bike"

Where kidbike is the crawling session id and other part is the target query from browser url.

This command put next requests to the job's queue:

And then check (with username: admin and pass: admin):

Increase crawler instances to speedup:

$ docker-compose scale crawler=2

Start another one crawling session:

$ docker-compose run --rm crawler manage session mountainbike "search/bia?is_paid=all&search_distance_type=mi&query=mountain+bike"

Project structure

$ tree -I "*.pyc|__pycache__"
.
├── base-compose.yml
├── craigslist
│   ├── crawler.py
│   ├── downloader.py
│   ├── __init__.py
│   ├── item.py
│   ├── log.py
│   ├── manage.py
│   ├── middleware.py
│   ├── pipeline.py
│   ├── queue.py
│   └── utils.py
├── dashboard.json
├── dataview
│   ├── __init__.py
│   ├── items
│   │   ├── admin.py
│   │   ├── apps.py
│   │   ├── __init__.py
│   │   ├── management
│   │   │   └── commands
│   │   │       ├── dbimport.py
│   │   │       └── __init__.py
│   │   ├── migrations
│   │   │   ├── 0001_initial.py
│   │   │   └── __init__.py
│   │   ├── models.py
│   │   ├── tests.py
│   │   └── views.py
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
├── docker-compose.yml
├── README.md
├── requires.pip
├── setup.py
└── tests
    ├── data
    │   ├── item.html
    │   └── list.html
    ├── test_crawler.py
    ├── test_downloader.py
    ├── test_pipeline.py
    ├── test_queue.py
    └── utils.py

8 directories, 37 files

TODO

License

(The MIT License)

Copyright (c) 2016 Evgeniy Tatarkin

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.