import urllib from HTMLParser import HTMLParser class TitleParser(HTMLParser): def __init__(self): self.title = '' self.divcontent = '' self.readingtitle = 0 self.readingdiv = 0 HTMLParser.__init__(self) def handle_starttag(self, tag, attrs): if tag == 'title': self.readingtitle = 1 if -1 != tag.find("div"): self.readingdiv = 1 def handle_data(self, data): if self.readingtitle: # Ordinarily, this is slow and a bad practice, but # we can get away with it because a title is usually # small and simple. self.title += data if self.readingdiv: self.divcontent += data def handle_endtag(self, tag): if tag == 'title': self.readingtitle = 0 if tag == "div": self.readingdiv = 0 def gettitle(self): return self.title def getdiv(self): return self.divcontent def getweb(url): web = urllib.urlopen('http://blog.chinaunix.net/u3/105068/showart_2223566.html').read() return web web = getweb('http://blog.chinaunix.net/u3/105068/showart_2223566.html') test = TitleParser() test.feed(web) file_object = open('abinfile', 'w') file_object.write(test.title) file_object.write("/r/n") file_object.write(test.divcontent) file_object.close()