How To Parse Yahoo Finance News Feed With Python

In [1]:
import json

To parse rss feed, we will use feedparser package.

Let us import the package first.

In [2]:
import feedparser
In [3]:
headers = {
  'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; rv:91.0) Gecko/20100101 Firefox/91.0'
}

Let us try to read the rss feed for the stock 'snow'

In [4]:
ticker = 'snow'
In [5]:
rssfeedurl = 'https://feeds.finance.yahoo.com/rss/2.0/headline?s=%s&region=US&lang=en-US'%ticker
In [6]:
NewsFeed = feedparser.parse(rssfeedurl)

NewsFeed is like a Python dictionary, we can use keys and indices to access it.

In [7]:
type(NewsFeed)
Out[7]:
feedparser.util.FeedParserDict
In [8]:
NewsFeed.keys()
Out[8]:
dict_keys(['bozo', 'entries', 'feed', 'headers', 'href', 'status', 'encoding', 'version', 'namespaces'])

Let us check how many entries we have.

In [9]:
len(NewsFeed.entries)
Out[9]:
20

Let us check our first news item.

In [10]:
NewsFeed.entries[0]
Out[10]:
{'summary': "The sharp dive that growth stocks experienced during the period came from fear of a recession hitting the economy as investors sought seemingly safer places to put their money.  It's only because their businesses still offer significant growth opportunities that investors should consider buying this pair of red-hot growth stocks and then hold onto them for years to come.  Programmatic ad-buying platform The Trade Desk (NASDAQ: TTD) might not fit the picture investors have of a barn-burner stock as shares are down 30% in 2022 and off more than 40% from their 52-week highs on worries a slowing economy is denting digital ad buying.",
 'summary_detail': {'type': 'text/html',
  'language': None,
  'base': 'https://feeds.finance.yahoo.com/rss/2.0/headline?s=snow&region=US&lang=en-US',
  'value': "The sharp dive that growth stocks experienced during the period came from fear of a recession hitting the economy as investors sought seemingly safer places to put their money.  It's only because their businesses still offer significant growth opportunities that investors should consider buying this pair of red-hot growth stocks and then hold onto them for years to come.  Programmatic ad-buying platform The Trade Desk (NASDAQ: TTD) might not fit the picture investors have of a barn-burner stock as shares are down 30% in 2022 and off more than 40% from their 52-week highs on worries a slowing economy is denting digital ad buying."},
 'id': '7f906078-bd3d-3c18-a3ef-362c0b5c4b89',
 'guidislink': False,
 'links': [{'rel': 'alternate',
   'type': 'text/html',
   'href': 'https://finance.yahoo.com/m/7f906078-bd3d-3c18-a3ef-362c0b5c4b89/2-red-hot-growth-stocks-to.html?.tsrc=rss'}],
 'link': 'https://finance.yahoo.com/m/7f906078-bd3d-3c18-a3ef-362c0b5c4b89/2-red-hot-growth-stocks-to.html?.tsrc=rss',
 'published': 'Sun, 11 Sep 2022 12:11:00 +0000',
 'published_parsed': time.struct_time(tm_year=2022, tm_mon=9, tm_mday=11, tm_hour=12, tm_min=11, tm_sec=0, tm_wday=6, tm_yday=254, tm_isdst=0),
 'title': '2 Red-Hot Growth Stocks to Buy in 2022 and Beyond',
 'title_detail': {'type': 'text/plain',
  'language': None,
  'base': 'https://feeds.finance.yahoo.com/rss/2.0/headline?s=snow&region=US&lang=en-US',
  'value': '2 Red-Hot Growth Stocks to Buy in 2022 and Beyond'}}

We can access any item from this news item using keyname.

In [11]:
NewsFeed.entries[0].summary
Out[11]:
"The sharp dive that growth stocks experienced during the period came from fear of a recession hitting the economy as investors sought seemingly safer places to put their money.  It's only because their businesses still offer significant growth opportunities that investors should consider buying this pair of red-hot growth stocks and then hold onto them for years to come.  Programmatic ad-buying platform The Trade Desk (NASDAQ: TTD) might not fit the picture investors have of a barn-burner stock as shares are down 30% in 2022 and off more than 40% from their 52-week highs on worries a slowing economy is denting digital ad buying."