-
-
Save matrixfox/f61fc1df5542472f1766 to your computer and use it in GitHub Desktop.
htmlparser
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
from HTMLParser import HTMLParser | |
import urllib2 | |
class MyHTMLParser(HTMLParser): | |
def __init__(self): | |
self.data=[] | |
self.href=0 | |
self.linkname='' | |
HTMLParser.__init__(self) | |
def handle_starttag(self, tag, attrs): | |
if tag == 'a': | |
for name,value in attrs: | |
if name == 'href': | |
self.href=1 | |
def handle_endtag(self,tag): | |
if tag=='a': | |
self.linkname=''.join(self.linkname.split()) | |
self.linkname=self.linkname.strip() | |
if self.linkname: | |
self.data.append(self.linkname) | |
self.linkname='' | |
self.href=0 | |
def handle_data(self, data): | |
if self.href: | |
self.linkname+=data | |
def getresult(self): | |
for value in self.data: | |
print value | |
def main(): | |
parser = MyHTMLParser() | |
url = 'http://flickr.com' | |
response = urllib2.urlopen(url) | |
parser.feed(response.read()) | |
parser.getresult() | |
parser.close() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
http://stackoverflow.com/questions/753052/strip-html-from-strings-in-python
and,
https://docs.python.org/2/library/htmlparser.html