-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNews_API.PY
More file actions
35 lines (23 loc) · 841 Bytes
/
Copy pathNews_API.PY
File metadata and controls
35 lines (23 loc) · 841 Bytes
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
NEWS API
#Use the NewsAPI and the requests module to fetch the daily news related to different topics.
#Go to: https://newsapi.org/ and explore the various options to build you application
SOLUTION:
import requests
query = input("What type of news are you interested in?: ")
# Set up the request parameters
url = 'https://newsapi.org/v2/top-headlines'
params = {
'country': 'us',
'apiKey': '52e5eb7afc1f45f5934fe377d11a9d04'
}
# Send the request to the API endpoint
response = requests.get(url, params=params)
# Parse the response JSON data
data = response.json()
# Print the news articles
for article in data['articles']:
print(article['title'])
print(article['description'])
print(article['url'])
print()
print("--------------------------------------")