Created
October 15, 2011 17:13
-
-
Save nus/1289855 to your computer and use it in GitHub Desktop.
HTMLParserを使ったページタイトルの取得
This file contains 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 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