EP Registry Scraper
A Python-based web scraper that extracts text and Eurovoc metadata from European Parliament (EP) documents through the EU Publications Office API.
Overview
This tool automates the collection of EU legislative documents from the CELLAR (Commission des EuropΓ©ennes Legislation Archive and Repository) system. It extracts document text in multiple formats (PDF, DOCX, XHTML) and enriches documents with Eurovoc thesaurus concept labels and their corresponding unique identifiers.
Features
- SPARQL-based document discovery: Queries the EU Publications Office SPARQL endpoint to find documents by date range
- Multi-format text extraction: Supports PDF, DOCX, DOC, and XHTML document formats
- Eurovoc enrichment: Automatically adds Eurovoc concept labels and IDs to documents
- Parallel processing: Uses multiprocessing for faster document text extraction
- Caching: Joblib-based caching to avoid redundant API calls and document downloads
- Robust error handling: Comprehensive logging of failures with detailed error messages
- Compressed output: Handles gzip-compressed JSONL files
Directory Structure
ep_registry_scrapper/
βββ README.md # This file
βββ requirements.txt # Python dependencies
βββ update.py # Main scraper script
βββ add_id.py # Post-processing script to add Eurovoc IDs
βββ get_eurovoc_dict.py # Utility to fetch and cache Eurovoc data
βββ process_all.sh # Bash wrapper for complete pipeline
βββ query.j2 # Jinja2 template for SPARQL queries (required)
Installation
Prerequisites
- Python 3.7+
- pip or conda
Setup
- Clone or download the repository
- Install dependencies:
pip install -r requirements.txt
- Note: You'll need a
query.j2template file that defines the SPARQL query for document discovery. This should contain a Jinja2 template with{{ start }}and{{ end }}date variables.
Usage
1. Extract Documents and Text
Extract documents from CELLAR and their text content:
python update.py <output_prefix> <num_days>
Arguments:
output_prefix: Prefix for output JSONL files (organized by month)num_days: Number of days to look back from today
Example:
python update.py output_ 30
This will create files like output_2025-11.jsonl for the last 30 days.
2. Fetch Eurovoc Dictionary
Generate a pickled dictionary mapping Eurovoc terms to their IDs:
python get_eurovoc_dict.py
Output: eurovoc_dict.pkl
3. Add Eurovoc IDs to Documents
Enrich existing JSONL files with Eurovoc concept IDs:
python add_id.py <input_file> <output_file>
Arguments:
input_file: Input JSONL file (can be/dev/stdinfor piping)output_file: Output JSONL file
Example:
python add_id.py documents.jsonl documents_with_ids.jsonl
4. Batch process to add IDs to multiple jsonls
If you need to add Eurovoc IDs to multiple JSONL files, use a shell loop:
for file in output_*.jsonl; do
python add_id.py "$file" "${file%.jsonl}_with_ids.jsonl"
done
5. Generate Eurovoc metrics
Create language barplots, document length per language boxplots
python analyse.py
Complete Pipeline
The main workflow is simply:
python update.py output_ 30
This extracts documents and text for the last 30 days.
If Eurovoc IDs were not appended during extraction (or want to update a already generated file), you can enrich the documents afterwards:
Fetch the Eurovoc dictionary (one-time setup):
python get_eurovoc_dict.pyAdd IDs to your JSONL files:
python add_id.py documents.jsonl documents_with_ids.jsonlOr use the bash wrapper to automate the entire pipeline:
bash process_all.sh
This ensures all steps are executed in the correct order.
Updating the IDs of all documents is recommended as labels and IDs can change over time
Output Format
The scraper produces JSONL (JSON Lines) files with the following structure per document:
{
"url": "https://publications.europa.eu/...",
"title": "Document Title",
"date": "2025-11-20",
"lang": "en",
"formats": ["pdf", "xhtml"],
"eurovoc_concepts": ["term1", "term2", ...],
"eurovoc_concepts_ids": ["id1", "id2", ...],
"text": "Extracted document text..."
}
Logging
All operations are logged to collect.log in the script directory. This includes:
- Document processing status
- Missing Eurovoc terms
- Download and parsing failures
- API errors
Monitor logs for issues:
tail -f collect.log
API Dependencies
- EU Publications Office SPARQL Endpoint:
https://publications.europa.eu/webapi/rdf/sparql - Eurovoc Dataset API:
http://publications.europa.eu/resource/dataset/eurovoc
Performance Considerations
- Caching: Downloads are cached for 2 hours to avoid redundant API calls
- Parallel Processing: Uses 16 worker processes for text extraction
- Rate Limiting: Consider adding delays if processing large date ranges
Dependencies
See requirements.txt for the complete list. Key packages:
requests: HTTP client for API callsbeautifulsoup4: HTML/XML parsingpdfminer.six: PDF text extractiondocx2txt: Microsoft Word document extractionjinja2: Template rendering for SPARQL queriesjoblib: Caching and parallel processingtqdm: Progress barsxmltodict: XML parsing