Extracting Financial News seamlessly using Python | by Nikhil Adithyan | CodeX | Nov, 2021

A hands-on tutorial on using EOD Historical Data’s API for financial news

Nikhil Adithyan
Photo by Roman Kraft on Unsplash
Table of Contents:
-
Introduction
- Extracting Financial News with Python
-- Importing Packages
-- Basic Program for Extracting Financial News
-- Advanced Program for Extracting Financial News
- Closing Notes

1. Importing Packages

pip install requests
pip install termcolor
import requests
from termcolor import colored as cl
api_key = 'YOUR API KEY'

2. Basic Program for Extracting Financial News

def get_stock_news(stock, api_key):
url = f'https://eodhistoricaldata.com/api/news?api_token={api_key}&s={stock}'
news_json = requests.get(url).json()

news = []

for i in range(10):
title = news_json[-i]['title']
news.append(title)
print(cl('{}. '.format(i+1), attrs = ['bold']), '{}'.format(title))

return news

amzn_news = get_stock_news('AMZN', api_key)

https://eodhistoricaldata.com/api/news?api_token={api_key}&s={stock}
Image by Author

3. Advanced Program for Extracting Financial News

def get_customized_news(stock, start_date, end_date, n_news, api_key, offset = 0):
url = f'https://eodhistoricaldata.com/api/news?api_token={api_key}&s={stock}&limit={n_news}&offset={offset}&from={start_date}&to={end_date}'
news_json = requests.get(url).json()

news = []

for i in range(len(news_json)):
title = news_json[-i]['title']
news.append(title)
print(cl('{}. '.format(i+1), attrs = ['bold']), '{}'.format(title))

return news

tsla_news = get_customized_news('TSLA', '2021-11-09', '2021-11-11', 15, api_key, 0)

https://eodhistoricaldata.com/api/news?api_token={api_key}&s={stock}&limit={n_news}&offset={offset}&from={start_date}&to={end_date}
Image by Author