Python, and the reacquaintance.


Oh, yeah, this is a small Python script to grab comments from the source of a webpage.

from sgmllib import SGMLParser
class CommentParser(SGMLParser):
	"Extract comments from webpage."
	def reset(self):
		self.comments = []
		SGMLParser.reset(self)
		
	def handle_comment(self, text):
		"""Push comments into the comments list."""
		self.comments.append("%(text)s" % locals())
	
	def display(self):
		"""Returned parsed comments as raw text."""
		return "".join(self.comments)
	
if __name__ == "__main__":
	import urllib
	import sys
	
	try: 
		host = sys.argv[1]
	except IndexError:
		host = 'http://ayaz.pk/'
		
	try:
		w = urllib.urlopen(host)
	except IOError:
		print "The webapge '%s' cannot be retrieved." % host
		sys.exit
	else:
		comment_parser = CommentParser()
		comment_parser.feed(w.read())
		w.close()
		comment_parser.close()
		
		print comment_parser.display()
		
		sys.exit	

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s