Fetching top news using News API

News API is a simple HTTP REST API for searching and retrieving live news articles from all over the web. Using this, one can fetch the top stories running on any 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 above idea :

filter_none

edit
close

play_arrow

link
brightness_4
code

# importing requests package
import requests     
  
def NewsFromBBC():
      
    # BBC news api
  
    # fetching data in json format
    open_bbc_page = requests.get(main_url).json()
  
    # getting all articles in a string article
    article = open_bbc_page["articles"]
  
    # empty list which will 
    # contain all trending news
    results = []
      
    for ar in article:
        results.append(ar["title"])
          
    for i in range(len(results)):
          
        # printing all trending news
        print(i + 1, results[i])
  
    #to read the news out loud for us
    from win32com.client import Dispatch
    speak = Dispatch("SAPI.Spvoice")
    speak.Speak(results)                 
  
# Driver Code
if __name__ == '__main__':
      
    # function call
    NewsFromBBC() 

chevron_right


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 time.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : araxk19

Article Tags :

5


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.