forked from suryanshsk/Python-Voice-Assistant-Suryanshsk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
updatednews_info.py
50 lines (39 loc) · 1.61 KB
/
updatednews_info.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import requests
def get_news(api_key = 'YOUR_API_KEY', category=None, country='us', num_articles=5):
"""
Fetches the latest news headlines from the News API.
Args:
api_key (str): Your News API key.
category (str): Optional news category (e.g., business, technology).
country (str): Country code for news (default is 'us').
num_articles (int): Number of articles to retrieve (default is 5).
Returns:
list: A list of news article titles.
"""
url = f'https://newsapi.org/v2/top-headlines?apiKey={api_key}&country={country}'
if category:
url += f"&category={category}"
try:
response = requests.get(url)
response.raise_for_status() # Raises an error for bad responses (4xx and 5xx)
data = response.json()
articles = data.get('articles', [])
# Handle case where no articles are returned
if not articles:
print("No articles found.")
return []
# Return the specified number of articles
return [article['title'] for article in articles[:num_articles]]
except requests.exceptions.HTTPError as http_err:
print(f"HTTP error occurred: {http_err}")
except requests.exceptions.RequestException as req_err:
print(f"Request error occurred: {req_err}")
except KeyError as key_err:
print(f"Key error: {key_err}")
return []
# Example usage:
if __name__ == '__main__':
api_key = 'YOUR_API_KEY' # Replace with your actual API key
headlines = get_news(api_key, category='technology')
for title in headlines:
print(title)