Skip to content

Instantly share code, notes, and snippets.

@evenh
Last active August 29, 2015 13:56
Show Gist options
  • Save evenh/9204524 to your computer and use it in GitHub Desktop.
Save evenh/9204524 to your computer and use it in GitHub Desktop.
A small Python script to fetch a random programming excuse
#!/usr/bin/env python
import urllib2
from HTMLParser import HTMLParser
# A simple script to get a quote from programmingexcuses.com
# by Even Holthe
#
# Can be invoked like this: curl -L -s http://tinyurl.com/programmingexcuses | python
class PEParser(HTMLParser):
def __init__(self):
HTMLParser.__init__(self)
self.recording = 0
self.data = None
def handle_starttag(self, tag, attrs):
if tag == 'a':
self.recording = 1
def handle_endtag(self, tag):
if tag == 'a':
self.recording -=1
def handle_data(self, data):
if self.recording:
self.data = data
parser = PEParser()
parser.feed(urllib2.urlopen('http://programmingexcuses.com/').read())
print parser.data
parser.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment