A robust Python web scraper for collecting company information from the Portuguese IMPIC public directory.
The scraper is designed for long-running data extraction tasks and includes automatic retry handling, checkpoint recovery, duplicate prevention, and structured CSV export.
- Extracts company information from the IMPIC public directory
- Supports filtering by district and company classification
- Automatic retry mechanism for temporary server errors
- Session-based HTTP requests for improved performance
- Checkpoint recovery to resume interrupted scraping sessions
- Failed ID tracking for later retries
- Duplicate detection
- Clean CSV export
- Modular project architecture
- Configurable scraping parameters
Each record contains:
- Company Name
- Company ID
- District
- Address
- Postal Code
- City
- Phone Number
- Website
- Company Classification
- Additional available details
impic_scraper/
│
├── src/
│ ├── scraper.py
│ ├── parser.py
│ ├── storage.py
│ ├── config.py
│ └── ...
│
├── output/
│
├── tests/
│
├── requirements.txt
├── README.md
└── LICENSE
Clone the repository:
git clone https://github.com/yourusername/impic-scraper.git
cd impic-scraperCreate a virtual environment (recommended):
python -m venv .venvActivate it:
Windows
.venv\Scripts\activateLinux / macOS
source .venv/bin/activateInstall dependencies:
pip install -r requirements.txtRun the scraper:
python -m src.mainDepending on the configuration, the scraper will:
- Fetch company pages
- Parse company details
- Save extracted records
- Track progress
- Resume automatically if interrupted
Scraping behavior can be adjusted inside:
src/config.py
Examples include:
- Request delay
- Retry attempts
- Timeout
- Output location
- District selection
- Classification filters
The scraper is designed to handle unstable network conditions.
Implemented mechanisms include:
- Automatic retries
- HTTP status validation
- Exception handling
- Failed request logging
- Resume after interruption
Long scraping jobs can safely resume after interruption.
Progress is periodically saved, allowing the scraper to continue from the last processed record instead of restarting from the beginning.
Scraped data is exported as CSV.
Example:
Company Name,Address,City,Phone,Website,...
ABC Company,Lisbon Street 10,Lisbon,+351...,https://...
- Python 3
- Requests
- BeautifulSoup
- CSV
- HTTP Sessions
The project follows a modular architecture with clear separation of responsibilities.
- Scraper handles HTTP communication
- Parser extracts structured data
- Storage manages persistence
- Configuration is centralized
This design improves readability, maintainability, and future extensibility.
- Parallel scraping
- SQLite/PostgreSQL storage
- Proxy rotation
- Automatic captcha handling
- Docker support
- Command-line arguments
- JSON export
- Unit tests
This project was created for educational purposes and legitimate data extraction from publicly accessible information.
Users are responsible for complying with the target website's Terms of Service and applicable laws.