Fetch top news of the day using Python
Hi guys, Today we will be going to discuss a simple project in Python language which is to read the top news of the day of a particular country.
Python is a very powerful language. Today there are many things in this world that you can do just by writing a few lines in Python language. One of these tasks is to get the top news of the day.
One of the most important parts of this project is to get the news API which is very helpful. Basically, an API (Application Program Interface)is a set of tools and routines for building software applications. An API makes it easier to develop a program by providing all the building blocks.
Let’s start by installing the requests module. This module allows you to send HTTP requests. In simple words it allows you to load the API in your program.
pip install requests import requests
Now give the http request and store the return result in the form of JSON file.
our_url = "https://newsapi.org/v2/top-headlines?country=in&apiKey=10f27a32c3224f959563a9964bbd70db" data = requests.get(our_url).json()
You can replace the API Key with your own API credentials. You can get your own news API key from https://newsapi.org/.
Now create an empty list to store the news articles. Print the list of articles separated by a new line and numbered accordingly.
See the full below code.
import requests def top_news(): our_url = "https://newsapi.org/v2/top-headlines?country=in&apiKey=10f27a32c3224f959563a9964bbd70db" data = requests.get(our_url).json() article = data["articles"] result=  for text in article: result.append(text["title"]) for i in range(len(results)): # printing all trending news print(i + 1, result[i]) if __name__ == '__main__': top_news()
1 Virat Kohli looks to avoid unwanted record as India gear up for Christchurch Test - India Today 2 Higher intakes of dairy milk associated with breast cancer risk: Study - Northeast Now 3 Delhi violence | Death toll rises to 24 - The Hindu 4 'Never Had Riots Here in 1984, 1992 Either': Amidst Violence, Seelampur Emerges an Oasis of Calm - The Wire 5 More new daily virus cases outside China than inside: WHO - The Hindu 6 Kejriwal will visit riot-hit areas to meet victims as per court order - Hindustan Times 7 India was great, trip very successful: Donald Trump - Times of India 8 Tennis star Maria Sharapova announces retirement - Times of India 9 ICC Women’s T20 World Cup: Pakistan begin campaign with thumping 8-wicket win over West Indies - Hindustan Times 10 "Don't Worry About Money, He Said": Family On Intel Officer's Killing - NDTV News 11 Scientists discovered the first animal that doesn't need oxygen to live. It's changing the definition of what an animal can be - CNN 12 UP: Azam Khan, wife, son sent to judicial custody - Times of India 13 Mars lander reveals new details about the Red Planet's strange magnetic field - Space.com 14 [Update: Processor Specification] Realme 6 series will have a 64MP main camera, 90Hz display, and 30W charging - XDA Developers 15 WhatsApp’s Dark Mode closer to official launch, key things to know about the feature - Hindustan Times 16 From 'Tere Naam' hair cut to his lucky blue bracelet - 6 trends that Salman Khan set - Times of India 17 Give Board Exam Schedule For 10-15 Days, CBSE Told Amid Delhi Violence - NDTV News 18 Did humans in India survive a super-volcano eruption 74,000 years ago? - Deccan Herald 19 Market LIVE: Sensex slips below 40,000-mark; SBI Cards IPO opens next week, should you invest? - The Financial Express 20 BREAKING! Shah Rukh Khan-Rajkumar Hirani’s Next To Begin In THIS Month; Location & Other Deets Out - Koimoi
The above output is the top news of the day the article was written. Just get the fresh API from https://newsapi.org/ to get the top news of some other day or just run again the above code on that day.
To get the top news of some other country just replace the country code in(for India) with other countries’ code like us for the USA.