News API is a simple JSON-based REST API for searching and retrieving news articles from all over the web. Using this, one can fetch the top stories running on a news website or can search top news on a specific topic (or keyword).
News can be retrieved based on some criteria. Say the topic (keyword) to be searched is ‘Geeksforgeeks’ or might be concerned to a specific channel. All can be done, but the API key is needed to get started.
Steps :
1. Visit https://newsapi.org/ to get your own API key.
2. Install requests package.
Below is the implementation of the above idea :
Python3
import requests
def NewsFromBBC():
query_params = {
"source" : "bbc-news" ,
"sortBy" : "top" ,
"apiKey" : "4dbc17e007ab436fb66416009dfb59a8"
}
res = requests.get(main_url, params = query_params)
open_bbc_page = res.json()
article = open_bbc_page[ "articles" ]
results = []
for ar in article:
results.append(ar[ "title" ])
for i in range ( len (results)):
print (i + 1 , results[i])
from win32com.client import Dispatch
speak = Dispatch( "SAPI.Spvoice" )
speak.Speak(results)
if __name__ = = '__main__' :
NewsFromBBC()
|
Output :
1 Italy to lift coronavirus travel restrictions
2 White House 'Operation Warp Speed' to look for Covid jab
3 Two Americas in the nation's capital
4 Kobe Bryant helicopter crash post-mortem released
5 Little things people are doing while socially distanced
6 The last 'normal' photo on your phone
7 'They came to kill the mothers'
8 EU-UK Brexit trade talks in trouble
9 Trial starts to see if dogs can 'sniff out' virus
10 Beatles photographer Astrid Kirchherr dies aged 81
Note: Output may change based on the top articles at the time.
Approach 2:
This approach will work as a cherry on a cake because it will display the news according to the category entered by the user so as work as a filter for those who want to know the news in a particular category/section.
First of all install newsapi and pycountry in your system if it’s not there the install using the command below:
~ pip install newsapi-python
~ pip install pycountry
Python3
from newsapi import NewsApiClient
import pycountry
newsapi = NewsApiClient(api_key = 'Your API Key' )
input_country = input ( "Country: " )
input_countries = [f '{input_country.strip()}' ]
countries = {}
for country in pycountry.countries:
countries[country.name] = country.alpha_2
codes = [countries.get(country.title(), 'Unknown code' )
for country in input_countries]
option = input ( "Which category are you interested in?\n1.Business\n2.Entertainment\n3.General\n4.Health\n5.Science\n6.Technology\n\nEnter here: " )
top_headlines = newsapi.get_top_headlines(
category = f '{option.lower()}' , language = 'en' , country = f '{codes[0].lower()}' )
Headlines = top_headlines[ 'articles' ]
if Headlines:
for articles in Headlines:
b = articles[ 'title' ][:: - 1 ].index( "-" )
if "news" in (articles[ 'title' ][ - b + 1 :]).lower():
print (
f "{articles['title'][-b+1:]}: {articles['title'][:-b-2]}." )
else :
print (
f "{articles['title'][-b+1:]} News: {articles['title'][:-b-2]}." )
else :
print (
f "Sorry no articles found for {input_country}, Something Wrong!!!" )
option = input ( "Do you want to search again[Yes/No]?" )
if option.lower() = = 'yes' :
continue
else :
exit()
|
Input:
Country: India
Which category are you interested in?
1.Business
2.Entertainment
3.General
4.Health
5.Science
6.Technology
Enter here: Technology
Output:
Gizbot News: Garena Free Fire Redeem Codes For August 16; Get Master of Minds Weapon Loot Crate.
News18: Google Pixel 5a Will Have Biggest Battery On Any Pixel Phone Ever, Launch This Month.
Hindustan Times News: PUBG Mobile: Here’s how to get the Unhinged Mortician set on PUBG Mobile, check details of RPM2 Royale Pass.
Times of India News: New iPhones, Watch, AirPods and more: What Apple may launch in September.
GSMArena.com News: Top 10 trending phones of week 32 - GSMArena.com news.
Hindustan Times News: Study finds if 'people persons' are 'machine persons' when they interact online.
Times of India News: 2021 vs 2020: August gasps for one ‘good’ air day in Gurugram this year.
ScienceAlert News: A Simple Crystal Could Finally Give Us Large-Scale Quantum Computing, Scientists Say.
Engadget News: The Switch is the first console to sweep Japan's game sales chart in 33 years.
GSMArena.com News: Weekly poll: Samsung Galaxy Z Fold3, the new S Pen and the Galaxy Z Flip3 - GSMArena.com news.
Market Research Telecast News: How to import your WhatsApp chats to WhatsApp Plus 17.00 Heymods.
Notebookcheck.net News: Realme will finally debut its first-gen laptop later in August 2021.
NewsBytes: Redmi 10 officially teased; color options revealed.
The Siasat Daily News: Xiaomi removes anti-lost mode from Mi Mix 4.
Zee News: Good news for Motorola Razr owners! Smartphone is finally getting Android 11 update.
EssentiallySports News: Animal Crossing: New Horizons- Predictions for the Next Big Update.
Republic World News: Motorola Edge 20 price in India Leaked: Price to range between Rs 21,499 to Rs 29,999.
Eurogamer.net News: Assassinations have been temporarily removed from Halo Infinite because "people just turn them off".
EssentiallySports News: Call of Duty: Warzone- If You Can Win a Game Despite the Hackers, You Can See a Vanguard Teaser.
Techradar News: Facebook Messenger voice and video calls are getting end-to-end encryption.
Do you want to search again[Yes/No]? No