News api python
There are several news APIs available that you can use with Python to fetch and process news data. Here are a few popular ones:
- News API: News API provides access to news articles from over 80,000 sources, including The New York Times, CNN, and BBC News. You can use their API to search for articles, get the latest news, and even filter by topic or location.
Python library: newsapi-python
- Google News API: Google News API provides access to news articles from a wide range of sources. You can use their API to search for articles, get the latest news, and even filter by topic or location.
Python library: google-news-api
- Al Jazeera News API: Al Jazeera News API provides access to news articles from Al Jazeera and other sources. You can use their API to search for articles, get the latest news, and even filter by topic or location.
Python library: al-jazeera-news-api
- AP News API: AP News API provides access to news articles from the Associated Press and other sources. You can use their API to search for articles, get the latest news, and even filter by topic or location.
Python library: ap-news-api
- NPR News API: NPR News API provides access to news articles from NPR and other sources. You can use their API to search for articles, get the latest news, and even filter by topic or location.
Python library: npr-news-api
Here's an example of how you can use the News API to fetch the latest news articles in Python:
import requests
api_key = "YOUR_API_KEY"
news_api = "https://newsapi.org/v2/top-headlines"
params = {
"country": "us",
"category": "general",
"apiKey": api_key
}
response = requests.get(news_api, params=params)
if response.status_code == 200:
data = response.json()
articles = data["articles"]
for article in articles:
print(article["title"])
print(article["description"])
print(article["url"])
else:
print("Error:", response.status_code)
Make sure to replace YOUR_API_KEY
with your actual API key from the news API provider.
You can also use Python libraries like feedparser
and beautifulsoup4
to parse and extract news articles from RSS feeds and websites.
Here's an example of how you can use feedparser
to parse an RSS feed and extract news articles:
import feedparser
rss_url = "https://www.example.com/rss"
feed = feedparser.parse(rss_url)
for entry in feed.entries:
print(entry.title)
print(entry.link)
print(entry.description)
Note that some news APIs may have usage limits, so be sure to check the API documentation before using it in your Python script.