Skip to content

Add Python blog scraper with PostgreSQL storage (Dockerized) - #137

Open
Lekhaaa-S wants to merge 1 commit into
UniCourt:mainfrom
Lekhaaa-S:fix-rss-feed-scraper
Open

Add Python blog scraper with PostgreSQL storage (Dockerized)#137
Lekhaaa-S wants to merge 1 commit into
UniCourt:mainfrom
Lekhaaa-S:fix-rss-feed-scraper

Conversation

@Lekhaaa-S

Copy link
Copy Markdown

What this does

Scrapes blog posts from blog.python.org and stores them in a PostgreSQL
database, fully containerized with Docker.

Approach

  • Used the site's RSS feed (/rss.xml) instead of scraping raw HTML.
    RSS gives clean, structured data (title, link, author, date,
    description) without depending on the page's CSS/HTML structure,
    which is more fragile and breaks if the site redesigns.
  • Parsed the feed with Python's built-in xml.etree.ElementTree.
  • Stored results in a blogs table in PostgreSQL, with a UNIQUE
    constraint on url and ON CONFLICT ... DO UPDATE, so re-running the
    scraper updates existing rows instead of duplicating them.

Docker setup

  • Dockerfile: builds the Python environment, installing
    requirements.txt before copying the rest of the source (for proper
    layer caching).
  • docker-compose.yaml: runs two services — psql-db (Postgres) and
    scraper (the Python script). Added a healthcheck on psql-db so
    the scraper waits until the database is actually ready before
    connecting.

How to run

docker-compose up --build
docker logs workshop_python_container
docker exec -it psql-db psql -U postgres -d blogs_db -c "SELECT * FROM blogs LIMIT 10;"

Verified

  • Scraper successfully fetches and parses the RSS feed (HTTP status: 200)
  • 50 records saved to the database
  • No empty titles/urls/authors, no duplicate urls

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant