08 December 2015

CST205 Week 7

The best thing that we have learned so far in this course is the use of dictionaries in Python (aka Hashes).

For additional fun I wrote an online headline aggregator (but didn't use a dictionary in it):

#'print onlineAggregator()' - it will ask you for directory to save online file before pulling headlines

def onlineAggregator():
  localPath = setMediaPath()
  import urllib
  urllib.urlretrieve('http://otterrealm.com/category/news/', localPath+'\\news.html')
  string = open(localPath+'\\news.html', 'r').read()
  newString = "*** Otter Realm Breaking News! ****\n"
  index = 0
  while index < len(string):
    start = string.find('<h3>', index)
    if start == -1:
      break
    end = string.find('</h3>', index)
    newString += '> ' + string[start + 4:end] + '\n'
    index = end + 5
  return newString
I know this could have been done without downloading the file, but the assignment was opening local files and parsing them.

No comments: