This script gets data from the Comelec website and saves precinct-level election results as CSV files organized by region, province, city, barangay, and precincts.
The crawler steps through the site’s data: it first fetches all regions, then for each region fetches its provinces, then cities, then barangays, and finally precinct IDs. For each precinct it downloads the detailed vote counts and writes them to a CSV in the matching folder path. A small checkpoint file records the last precinct processed so you can stop and restart without losing progress.
- Request
…/regions/local/0.jsonto get all region codes. - For each region, request
…/regions/local/{region_code}.jsonto list its provinces. - For each province, request
…/regions/local/{province_code}.jsonto list its cities. - For each city, request
…/regions/local/{city_code}.jsonto list its barangays. - For each barangay, request
…/regions/precinct/{prefix}/{barangay_code}.jsonto get precinct IDs. - For each precinct ID, request
…/data/er/{prefix}/{precinct_id}.jsonto fetch vote details.
- Python 3.8 or later
- Clone the repository
- (Recommended) Create and activate a virtual environment
python3 -m venv venv source venv/bin/activate
Run python main.py
Data will be saved under the output folder
Thanks for checking out the repository. If you run into bugs, missing data, or have feature requests, please let me know by opening an issue. To make it easier for me to reproduce and fix the problem, please follow these guidelines:
- Bug – Something isn’t working as expected (errors, missing files, wrong CSV fields).
- Enhancement – A new feature or improvement (new output format, logging tweaks, performance).
- Question – Clarifications about usage, configuration, or internals.
When you click New Issue, select the matching label above.
Use a short, descriptive title that summarizes the problem:
Bug: precinct fetch fails for barangay code 3901000 Enhancement: allow setting custom output directory
- What you expected – e.g. “I expected
main.pyto skip already-downloaded precincts.” - What actually happened – e.g. “It fetched the same precinct again and overwrote the CSV.”
Please include logs, errors codes, stack traces, and other information as it will help me and others identify the issue that you are encountering.
List the exact steps you took. For example:
- Clone the repo at
commit abc123 - Create a venv, install
httpx - Run
python main.py - Observe that
output/NCR/.../123456.csvis not written
- Operating system & version (macOS 12, Ubuntu 22.04, Windows 11)
- Python version (
python --version) - HTTPX version (
pip show httpx) - Any special flags or config you used
If you saw error messages or stack traces, please please paste them in a code block:
2025-05-30 10:15:20 ERROR Failed to fetch https://…/123.json: 404 Client ErrorIt will greatly help me and others debug your code.
Title - Bug when fetching precinct data
Description -
When fetching precinct data for barangay code R001000 the script fails with a 404 error instead of skipping that precinct and continuing.
Steps to Reproduce -
1 Create and activate a virtual environment
2 Install dependencies with pip install -r requirements.txt
3 Run python main.py
4 Observe that precinct 3901000 triggers a 404 and stops the crawler
Expected Behavior -
The script should log the 404, skip that precinct, update the checkpoint, and continue with the next precinct
Actual Behavior -
The crawler stops execution on the 404 error and does not process any further precincts
Environment -
- Python version - 3.10.4
- OS - Ubuntu 22.04
- httpx version - 0.23.3
Logs or Screenshots -
2025-06-01 12-00-00 ERROR Failed to fetch https---2025electionresults.comelec.gov.ph/data/er/390/3901000.json 404 Client Error
Additional Context -
This happens consistently for any precinct code under that barangay and prevents completion of the crawl
Title - It does not work
Description -
The script fails
Steps to Reproduce -
Run the code
Expected Behavior -
It should work
Actual Behavior -
It does not
Environment -
Not sure
Logs or Screenshots -
No logs available