Skip to content

Instantly share code, notes, and snippets.

@jmcph4
Created November 16, 2014 06:21
Show Gist options
  • Select an option

  • Save jmcph4/00d7459bc2b1e2200631 to your computer and use it in GitHub Desktop.

Select an option

Save jmcph4/00d7459bc2b1e2200631 to your computer and use it in GitHub Desktop.
A small Python script that retrieves headlines from Bloomberg and counts the number of occurrences of a given keyword.
from bs4 import BeautifulSoup
import urllib2
def get_headlines():
url = "http://www.bloomberg.com/quickview"
html = urllib2.urlopen(url).read()
soup = BeautifulSoup(html)
headlines = {}
for link in soup.find_all('a', class_="q story_link black"):
headlines.update({link.text:link.get('href')})
return headlines
def print_headlines():
headlines = get_headlines()
for headline in headlines:
print(headline)
def count_occurrences(keyword):
headlines = get_headlines()
per_headline = 0
total = 0
for headline in headlines:
per_headline = headline.count(keyword)
total = total + per_headline
per_headline = 0
return total
keyword = raw_input("Enter keyword: ")
print(count_occurrences(keyword))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment