All pastes #10054 Raw Edit

Getting headlines

public python v1 · immutable
#10054 ·published 2005-04-21 23:45 UTC
rendered paste body
 #!/usr/bin/env pythonheadlines_url = "http://es.openoffice.org/servlets/ProjectNewsList"import HTMLParserimport urllib2class HeadlinesParser(HTMLParser.HTMLParser):	def __init__(self):		HTMLParser.HTMLParser.__init__(self)		self.headlines = {}		self.while_anchor = False		self.last_href = None			def handle_starttag(self, name, attrs):		attrs = dict(attrs)		if name == "a":			if "href" in attrs:				current_href = attrs["href"]				if "newsItemID" in current_href:					self.while_anchor = True					self.last_href = current_href			def handle_data(self, content):		content = content.strip()		if self.while_anchor:			self.headlines[content] = self.last_href		def handle_endtag(self, name):		if name == "a":			self.while_anchor = Falsedef main():	source = urllib2.urlopen(headlines_url).read()	parser = HeadlinesParser()	parser.feed(source)	print '''create table "Headlines"\n\t(headline text, url text);'''	for headline in parser.headlines.items():		print '''insert into "Headlines" values\n\t(\n\t '%s',\n\t '%s'\n\t);''' % headlineif __name__ == "__main__":	main()