Created
February 18, 2010 20:32
-
-
Save hktechn0/308029 to your computer and use it in GitHub Desktop.
pick out title from HTML
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 | |
# -*- coding: utf-8 -*- | |
import sys | |
import urllib2 | |
import HTMLParser | |
html = urllib2.urlopen(sys.argv[1]).read() | |
class titleparser(HTMLParser.HTMLParser): | |
title = unicode() | |
titleflg = False | |
def handle_starttag(self, tag, attrs): | |
if tag == "title": | |
self.titleflg = True | |
def handle_endtag(self, tag): | |
if tag == "title": | |
self.titleflg = False | |
def handle_data(self, data): | |
if self.titleflg: | |
self.title += data.decode("utf-8") | |
def handle_charref(self, ref): | |
if self.titleflg: | |
if ref.isdigit(): | |
self.title += unichr(int(ref)) | |
p = titleparser() | |
p.feed(html) | |
print p.title |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment