# 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 1. Clone or download the repository 2. Install dependencies: ```bash pip install -r requirements.txt ``` 3. Note: You'll need a `query.j2` template 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: ```bash python update.py ``` Arguments: - `output_prefix`: Prefix for output JSONL files (organized by month) - `num_days`: Number of days to look back from today Example: ```bash 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: ```bash python get_eurovoc_dict.py ``` Output: `eurovoc_dict.pkl` ### 3. Add Eurovoc IDs to Documents Enrich existing JSONL files with Eurovoc concept IDs: ```bash python add_id.py ``` Arguments: - `input_file`: Input JSONL file (can be `/dev/stdin` for piping) - `output_file`: Output JSONL file Example: ```bash 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: ```bash 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 ```bash python analyse.py ``` ## Complete Pipeline The **main workflow** is simply: ```bash 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: 1. Fetch the Eurovoc dictionary (one-time setup): ```bash python get_eurovoc_dict.py ``` 2. Add IDs to your JSONL files: ```bash python add_id.py documents.jsonl documents_with_ids.jsonl ``` 3. Or use the bash wrapper to automate the entire pipeline: ```bash 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: ```json { "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: ```bash 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 calls - `beautifulsoup4`: HTML/XML parsing - `pdfminer.six`: PDF text extraction - `docx2txt`: Microsoft Word document extraction - `jinja2`: Template rendering for SPARQL queries - `joblib`: Caching and parallel processing - `tqdm`: Progress bars - `xmltodict`: XML parsing