Skip to content

Instantly share code, notes, and snippets.

@nus
Created October 15, 2011 17:13
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nus/1289855 to your computer and use it in GitHub Desktop.
Save nus/1289855 to your computer and use it in GitHub Desktop.
HTMLParserを使ったページタイトルの取得
from HTMLParser import HTMLParser
import urllib2
class GetTitle(HTMLParser):
def __init__(self):
HTMLParser.__init__(self)
self.title_flag = False
def handle_starttag(self, tag, attrs):
if tag == 'title':
self.title_flag = True
def handle_data(self, data):
if self.title_flag:
self.title = data
self.title_flag = False
def main():
url = 'http://twitter.com'
response = urllib2.urlopen(url)
gt = GetTitle()
gt.feed(response.read())
gt.close()
print '%s - %s' % (url, gt.title)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment