Home News Fetching top news using News API

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
    main_url = " https://newsapi.org/v1/articles?source=bbc-news&sortBy=top&apiKey=4dbc17e007ab436fb66416009dfb59a8"

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

Check out this Author’s contributed articles.

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

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Recommended Posts:

(Login to Rate)

Average Difficulty : 0/5.0
No votes yet.

Writing code in comment? Please use , generate link and share the link here.