📰 Build a Python News Aggregator
Staying updated with Python news can be time-consuming. Instead of manually visiting multiple websites, you can build a Python News Aggregator that collects updates automatically.
📌 What is a News Aggregator?
A news aggregator collects articles from different sources and presents them in one place. Google News and Feedly are examples.
💡 Features of Our Python News Aggregator:
- ✅ Fetch latest Python news from multiple sources.
- ✅ Display article titles, summaries, and links.
- ✅ Allow users to filter news by keyword (e.g., AI, Django, Data Science).
- ✅ Provide a search bar for finding specific topics.
📡 1. Fetch News Using RSS Feeds
Most news websites provide RSS Feeds, which allow programs to retrieve their latest articles easily. We will use feedparser
to extract news.
🚀 Example: Fetch News from Python Insider
import feedparser
rss_url = "https://feeds.feedburner.com/PythonInsider"
feed = feedparser.parse(rss_url)
for entry in feed.entries[:5]: # Get top 5 news items
print(f"Title: {entry.title}")
print(f"Link: {entry.link}")
print(f"Summary: {entry.summary}\n")
Install feedparser:
pip install feedparser
🌎 2. Scrape News from Websites (For Non-RSS Sources)
Some sites don’t provide RSS feeds, so we can use BeautifulSoup to extract article titles.
🚀 Example: Scrape Dev.to for Python Articles
import requests
from bs4 import BeautifulSoup
url = "https://dev.to/t/python"
response = requests.get(url)
soup = BeautifulSoup(response.text, "html.parser")
articles = soup.find_all("h2", class_="crayons-story__title", limit=5)
for article in articles:
title = article.get_text(strip=True)
link = "https://dev.to" + article.find("a")["href"]
print(f"Title: {title}\nLink: {link}\n")
Install BeautifulSoup:
pip install beautifulsoup4 requests
📊 3. Build a Web App to Display News
We can now use Flask to create a simple web page that displays the latest Python news dynamically.
🚀 Flask Web App to Show News
from flask import Flask, render_template, request
import feedparser
app = Flask(__name__)
# Fetch news from RSS feeds
def fetch_news():
rss_urls = [
"https://feeds.feedburner.com/PythonInsider",
"https://realpython.com/atom.xml"
]
news_items = []
for rss_url in rss_urls:
feed = feedparser.parse(rss_url)
for entry in feed.entries[:5]: # Limit to 5 news per source
news_items.append({"title": entry.title, "link": entry.link, "summary": entry.summary})
return news_items
@app.route("/", methods=["GET"])
def home():
query = request.args.get("search", "")
news = fetch_news()
# Filter news based on search keyword
if query:
news = [item for item in news if query.lower() in item["title"].lower()]
return render_template("index.html", news=news, query=query)
if __name__ == "__main__":
app.run(debug=True)
Install Flask:
pip install flask
📜 4. HTML Template (index.html)
Save this as templates/index.html
:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Python News Aggregator</title>
</head>
<body>
<h1>📰 Latest Python News</h1>
<form method="GET">
<input type="text" name="search" placeholder="Search news..." value="{{ query }}">
<button type="submit">Search</button>
</form>
<ul>
{% for article in news %}
<li>
<h3><a href="{{ article.link }}" target="_blank">{{ article.title }}</a></h3>
<p>{{ article.summary[:150] }}...</p>
</li>
{% endfor %}
</ul>
</body>
</html>
🚀 Additional Features You Can Add
- ✅ Dark Mode Support: Add CSS for theme switching.
- ✅ More RSS Feeds: Add sources like Reddit (r/Python), Hacker News, and Medium Blogs.
- ✅ Database Storage: Use SQLite to store news articles.
- ✅ Email Notifications: Send daily Python news via email using SMTP.
🎯 Conclusion
With this Python News Aggregator, you can:
- ✅ Stay updated with the latest Python news.
- ✅ Search for specific topics easily.
- ✅ Expand it into a full-fledged website!
💬 What other tech news sources would you like to add? Let me know in the comments! 👇
📢 Stay updated with the latest Python trends – follow our blog for more insights! 🚀