Evaluating Yahoo Finance News with FeedParser and Alchemy

Everybody is on the Web today, so there is a lot of interest in sentiment analysis. I made a short script that retrieves financial news from a Yahoo Finance RSS feed for a given symbol. The RSS is parsed with FeedParser. I am now trying out the Alchemy API for sentiment analysis. For my first attempt I am just scoring the headlines of the last 12 hours (ignoring duplicates). I basically give 1 point for a “positive” headline and deduct 1 point for a negative headline. Alchemy limits free accounts to 1000 calls a day I believe so this is just temporary. As they say here is the code. Enjoy!

import feedparser as fp
import sys
from time import mktime
from datetime import datetime as dt
import calendar as cal
from alchemyapi import AlchemyAPI
base_url = "http://finance.yahoo.com/rss/headline?s="
def sentiment(alchemyapi, myText):
   response = alchemyapi.sentiment("text", myText)
   return response["docSentiment"]["type"]
def main():
   alchemy = AlchemyAPI()
   rss = fp.parse(base_url + sys.argv[1])
   title = ""
   i = 0
   score = 0
   for entry in rss.entries:
      pub_date = dt.fromtimestamp(mktime(entry.published_parsed))
      delta = dt.now() - pub_date
      if delta.seconds/3600 < 12:
         if title == entry.title:
            title = entry.title
         print "%d%s %d%d %s" % (pub_date.day, cal.month_abbr[pub_date.month], pub_date.hour, pub_date.minute, entry.title)
         mood = sentiment(alchemy, entry.title)
         if mood == "positive":
         elif mood == "negative":
         print mood, score, "/", i
if __name__ == "__main__":
By the author of NumPy Beginner's Guide, NumPy Cookbook and Instant Pygame. If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.
This entry was posted in programming and tagged . Bookmark the permalink.