Last active
August 29, 2015 13:56
-
-
Save evenh/9204524 to your computer and use it in GitHub Desktop.
A small Python script to fetch a random programming excuse
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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