[ create a new paste ] login | about

Project: python
Link: http://python.codepad.org/YGw1ISEG    [ raw code | fork ]

fedecarg - Python, pasted on Jun 14:
'''
Quick English to Spanish 1-word Translator
'''
import urllib2, sys, re

def translate(word, lang):
	print "WORD: "+word
	print "TRANSLATION TO LANGUAGE: "+lang
	print "-----------------------------------"
	sock = urllib2.urlopen("http://online.babylon.com/cgi-bin/trans.cgi?layout=txt&lang=spa&word="+word)
	htmlsource = sock.read()
	sock.close()

	#Replacing some specific html tags for \n in order to format the final output:
	htmlsource = htmlsource.replace("<hr>","\n");
	htmlsource = htmlsource.replace("<BR>","\n");
	htmlsource = htmlsource.replace("<br>","\n");
	htmlsource = htmlsource.replace("</BR>","");
	htmlsource = htmlsource.replace("</br>","");
	htmlsource = htmlsource.split(word)[1]

	#Removing unwanted html tags (acctually all the tags):
	iter = re.finditer(r'<.*?>',htmlsource)
	for m in iter:
		htmlsource = htmlsource.replace(m.string[m.start():m.end()],"")

	#More output formating
	tags = htmlsource.split("\n");
	output = ""
	for str in tags:
		output += str.strip()+"\n"

	#tweak this up for your language
	output = output.decode("iso-8859-1").encode("utf-8")	
	print output

try:
	word = sys.argv[1]
	lang = sys.argv[2]
	translate(word,"")

except(IndexError):
	if(word.__len__() != 0):
		translate(word, "")


Create a new paste based on this one


Comments: