How to Fetch Real-Time News in Python (3 Practical Examples)

Published: (December 30, 2025 at 07:34 PM EST)
4 min read
Source: Dev.to

Source: Dev.to

Prerequisites

  • Python 3.7+

  • requests library

    pip install requests
  • A free API key from NewsMesh (free tier available)

import requests

API_KEY = "your_api_key_here"
BASE_URL = "https://api.newsmesh.co/v1"

def get_trending_news(limit: int = 10):
    """Fetch currently trending news articles."""
    response = requests.get(
        f"{BASE_URL}/trending",
        params={"apiKey": API_KEY, "limit": limit}
    )
    response.raise_for_status()
    return response.json()["data"]

# Usage
for article in get_trending_news():
    print(f"📰 {article['title']}")
    print(f"   Source: {article['source']} | Category: {article['category']}")
    print(f"   Link: {article['link']}\n")

Sample Output

📰 Fed Signals Rate Cut in Early 2025
   Source: Reuters | Category: business
   Link: https://reuters.com/...

📰 OpenAI Announces GPT‑5 Preview
   Source: TechCrunch | Category: technology
   Link: https://techcrunch.com/...

Example 2: Filter News by Category and Country

def get_latest_news(category: str | None = None,
                    country: str | None = None,
                    limit: int = 10):
    """
    Fetch latest news with optional filters.

    Args:
        category: politics, technology, business, health, entertainment,
                  sports, science, lifestyle, environment, world
        country: ISO code like 'us', 'gb', 'de', 'fr', etc.
        limit: max 25 articles per request
    """
    params = {"apiKey": API_KEY, "limit": limit}
    if category:
        params["category"] = category
    if country:
        params["country"] = country

    response = requests.get(f"{BASE_URL}/latest", params=params)
    response.raise_for_status()
    return response.json()["data"]

# Technology news from the US
tech_news = get_latest_news(category="technology", country="us")

# Sports news from the UK
uk_sports = get_latest_news(category="sports", country="gb")

# All business news (max 25)
business = get_latest_news(category="business", limit=25)

Available Categories

CategoryDescription
politicsPolitical news
technologyTech, startups, gadgets
businessMarkets, finance, economy
healthMedical, wellness
entertainmentCelebrity, movies, music
sportsAll sports coverage
scienceScientific discoveries
lifestyleCulture, trends
environmentClimate, sustainability
worldInternational news

Supported Countries

35+ countries including: us, gb, ca, au, de, fr, jp, in, br, and more.

Example 3: Search News by Keyword

def search_news(query: str,
                from_date: str | None = None,
                to_date: str | None = None,
                sort_by: str = "date_descending"):
    """
    Search news articles by keyword.

    Args:
        query: Search term (e.g., "bitcoin", "climate change")
        from_date: Start date as YYYY‑MM‑DD
        to_date: End date as YYYY‑MM‑DD
        sort_by: 'date_descending', 'date_ascending', or 'relevant'
    """
    params = {
        "apiKey": API_KEY,
        "q": query,
        "sortBy": sort_by,
        "limit": 25
    }
    if from_date:
        params["from"] = from_date
    if to_date:
        params["to"] = to_date

    response = requests.get(f"{BASE_URL}/search", params=params)
    response.raise_for_status()
    return response.json()["data"]

# Search for Bitcoin news from the last week
bitcoin_news = search_news(
    query="bitcoin",
    from_date="2024-12-23",
    sort_by="relevant"
)

for article in bitcoin_news[:5]:
    print(f"• {article['title']}")
    print(f"  {article['published_date'][:10]} - {article['source']}\n")

Bonus: Simple News‑Monitoring Script

A practical script that monitors news for specific keywords and prints new articles as they appear.

import requests
import time
from datetime import datetime

API_KEY = "your_api_key_here"
KEYWORDS = ["AI", "OpenAI", "GPT"]   # Topics to monitor
CHECK_INTERVAL = 300                # Check every 5 minutes

seen_articles = set()

def check_for_news():
    """Check for new articles matching our keywords."""
    for keyword in KEYWORDS:
        response = requests.get(
            "https://api.newsmesh.co/v1/search",
            params={"apiKey": API_KEY, "q": keyword, "limit": 5}
        )
        if response.status_code != 200:
            continue

        for article in response.json().get("data", []):
            article_id = article["article_id"]
            if article_id not in seen_articles:
                seen_articles.add(article_id)
                print(f"\n🔔 NEW: {article['title']}")
                print(f"   Keyword: {keyword} | Source: {article['source']}")

if __name__ == "__main__":
    while True:
        check_for_news()
        time.sleep(CHECK_INTERVAL)
print(f"   Link: {article['link']}")
if __name__ == "__main__":
    print(f"Monitoring news for: {', '.join(KEYWORDS)}")
    print("Press Ctrl+C to stop\n")

    while True:
        check_for_news()
        time.sleep(CHECK_INTERVAL)

Handling Pagination

For fetching more than 25 articles, use the next_cursor returned in responses:

def get_all_articles(category, max_articles=100):
    """Fetch multiple pages of articles."""
    all_articles = []
    cursor = None

    while len(all_articles) < max_articles:
        params = {"apiKey": API_KEY, "category": category, "limit": 25}
        if cursor:
            params["cursor"] = cursor

        response = requests.get(f"{BASE_URL}/latest", params=params)
        data = response.json()

        all_articles.extend(data["data"])
        cursor = data.get("next_cursor")

        if not cursor:  # No more pages
            break

    return all_articles[:max_articles]

Error Handling Best Practices

Always handle API errors gracefully:

def safe_api_call(endpoint, params):
    """Make API call with proper error handling."""
    try:
        response = requests.get(endpoint, params=params, timeout=10)
        response.raise_for_status()
        return response.json()

    except requests.exceptions.Timeout:
        print("Request timed out. Try again.")
        return None

    except requests.exceptions.HTTPError as e:
        error_data = e.response.json()
        print(f"API Error: {error_data.get('message', 'Unknown error')}")
        return None

    except requests.exceptions.RequestException as e:
        print(f"Request failed: {e}")
        return None

What Can You Build?

Here are some ideas:

  • News digest bot – Daily email/Slack summary of topics you care about
  • Trading signals – React to financial news in real-time
  • Content aggregator – Build your own Google News
  • Research tool – Track coverage of specific companies or topics
  • News widget – Add a live news feed to your website

Wrapping Up

Fetching news in Python is straightforward once you have a reliable API. The examples above should get you started quickly.

Resources

What are you building with news data? Drop a comment below – I’d love to hear about your projects!

Back to Blog

Related posts

Read more »