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 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 specific channel. All can be done, but 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 :

# 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:
    for i in range(len(results)):
        # printing all trending news
        print(i + 1, results[i])                 
# Driver Code
if __name__ == '__main__':
    # function call

Output :

1 China hits back with tariffs on US imports
2 Trump rails at immigrant programme
3 'I have 29 textbooks for 87 pupils'
4 French rail strike set to test Macron
5 London murder rate overtakes New York's
6 Hill Street Blues creator dies aged 74
7 Villagers killed in Boko Haram clash
8 Kim 'moved' by K-pop peace concert
9 Former president's poisoning ordeal
10 Costa Rican president-elect wants 'unity'

Note : Output may change based on the top articles at time.

This article is attributed to GeeksforGeeks.org

You Might Also Like

leave a comment



load comments

Subscribe to Our Newsletter