Energy Research: Top News Energy

Getting Top News Energy Headlines and Stories using Eikon Data API (Python)

Raksina Samasiri
Developer Advocate Developer Advocate
Vetit Sirasupparakchai
Customer Success Manager Customer Success Manager

This article demonstrates how to retrieve Top News Energy headlines and stories using Python code with Eikon Data API

What is Top News Energy?

Top News Energy presents the biggest energy stories that are moving markets and making headlines worldwide in an easy-to-use format. The content is updated 24/7 by dedicated and experienced global and local editors.

To access Top News Energy, type “TOP/O” in the Eikon Search box

Then click on the headline *TOP NEWS* Energy to access it

Here's an example after clicking Full story of the first News headline

Prerequisite

You may contact your Refinitiv’s representative to help you to access the product

  • Eikon Data API
    • Eikon Desktop or Refinitiv workspace is running on your machine when running the notebook application
    • For how to start using Eikon Data API, you can follow its Quickstart guide
  • Jupyter Lab or Jupyter Notebook application is installed and ready to use
    • You could use Eikon Codebook as well

Required python libraries and their version:

Python version 3.9.6 is being used here

You may download requirements.txt from this repository then use the command pip install -r requirements.txt to install required libraries

  • eikon == 1.1.14
  • bs4 == 0.0.1
  • IPython == 7.27.0

Let's get started!

Step 1) import the necessary Python libraries and set the Eikon's App Key

This Quick Start guide contains the step to create Eikon's App Key

    	
            import eikon as ek
from IPython.display import HTML
from bs4 import BeautifulSoup

ek.set_app_key('#### YOUR EIKON APP KEY ####')

Step 2) Get the News headline of TOP NEWS Energy

Get news headline for "TOP/O", the output dataframe contains one headline that is *TOP NEWS* Energy
(Please note that *TOP NEWS* Energy story will be updated over time.)

    	
            df_news = ek.get_news_headlines(query = 'TOP/O', count=10)
display(df_news)

Step 3) Retrieve the content of Top News Energy

Get storyId of *TOP NEWS* Energy to be used to retrieve story body with get_news_story() function

    	
            story_id = df_news.loc[df_news['text'] == '*TOP NEWS* Energy']['storyId'][0]
print(story_id)

The output is story ID: urn:newsml:reuters.com:20171030:nTOPO:92
HTML function of IPython.display can be used to display the story after retrieved

    	
            top_news_story = ek.get_news_story(story_id)
HTML(top_news_story)

The list of News stories here is the same as the list shown in Eikon Desktop app at the beginning of this article

Below is the output of top_news_story, which contains

  1. Headlines under 'pre' tag
  2. Story IDs is in data-story-id under 'a' tag with data-type="storyId"

Step 3.1 ) Get headlines using the code below

Use BeautifulSoup library as HTML parsing text and get "data-story-id" which is the story that contain in Top NEWS story

    	
            soup = BeautifulSoup(top_news_story)

all_pre = soup.findAll('pre')

input_text = all_pre[0].text
list_headline = input_text.replace('> ', '').replace('(Full Story)','').split('\r\n')
list_headline = [headline.strip() for headline in list_headline]
print(list_headline)

Step 3.2 ) Get Top News stories

3.2.1 ) Get story Ids using the code below

    	
            list_topnews_story_id = []
for story_id in soup.findAll('a'):
if story_id.get('data-type') == 'storyId':
list_topnews_story_id.append(story_id.get('data-story-id'))
print(list_topnews_story_id)
    	
            ['nL2N2VP0K8', 'nL5N2VP28P', 'nL2N2VP11J', 'nL2N2VP1GD', 'nL5N2VO50L', 'nL2N2VP05F', 'nL2N2VP1KX', 'nL5N2VO51E', 'nS8N2LA03R', 'nL2N2VO12M', 'nL2N2VM088', 'nL5N2VP22U', 'nL2N2VO22N', 'nL3N2VL2CK', 'nL3N2VP3KY']
        
        
    

From the list of Story IDs returned above, we're going to retrieve their stories

3.2.1 ) Use story IDs to retrieve news story

To retrieve the latest news story

    	
            print(f'Story ID: {list_topnews_story_id[0]}')
latest_story = ek.get_news_story(list_topnews_story_id[0])
display(HTML(latest_story))

An example code to retrieve all news story in the list

The output can be found in this Notebook file

    	
            for story_id in list_topnews_story_id:
story = ek.get_news_story(story_id)
display(HTML(story))

Observations

Eikon Data API (Python) can be used to retrieve Top News Energy Headlines and Stories that can be applied to your workflow of new monitoring and more. Feel free to try it out.

The full code can be found in this GitHub Repository

Plus, to get other queries for top news, you can type "top news - " on the Eikon search bar to see that which keyword can be used.

  • Register or Log in to applaud this article
  • Let the author know how much this article helped you
If you require assistance, please contact us here