Feeds:
Posts
Comments

Posts Tagged ‘malayalam’

This time its the script to fetch the malayalam meanings of english words from malayalam wiktionary.

#!/usr/bin/python
#-*- coding: utf-8 -*-
# https://ragsagar.wordpress.com

from HTMLParser import HTMLParser
import urllib2,sys


class Parser(HTMLParser):
	
	def __init__(self):
		self.in_header = False
		self.in_definition = False
		HTMLParser.__init__(self)
	
	def handle_starttag(self, tag, attrs):
		if tag == 'li':
			self.in_header = True
		if tag == 'a' and self.in_header :
			self.in_definition = True
	
	def handle_endtag(self, tag):
	    	if tag == 'li':
			self.in_header = False
		if tag == 'a' :
			self.in_defintion = False
			
	def handle_data(self, data):
		if self.in_definition:
			print data 
			exit(0)
					


try :
	search_item = sys.argv[1]
except IndexError:
	print " syntax : %s <definiton> " % sys.argv[0]
	exit(1)
search_link = "http://ml.wiktionary.org/w/index.php?title=%s&printable=yes" % (urllib2.quote(search_item.encode('utf-8')))


request = urllib2.Request(search_link)
request.add_header('User-agent', 'Mozilla/5.0 (hb)')
try :
	response = urllib2.urlopen(request)
except urllib2.HTTPError :
	print "No definition found "
except urllib2.URLError :
	print "Unable to connect to internet "
else :
	parser = Parser()
	data = response.read()
	parser.feed(data)
			

Output in my terminal

malayalam meaning

Advertisements

Read Full Post »

%d bloggers like this: