coding utf-8 import urllib2 langs en fr es it de pt ru pl def translat

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# coding: utf-8
import urllib2
langs = ['en', 'fr', 'es', 'it', 'de', 'pt', 'ru', 'pl']
def translate(to_translate, to_langage="auto", langage="auto"):
agents = {
'User-Agent': "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30)"}
before_trans = 'class="t0">'
link = "http://translate.google.com/m?hl=%s&sl=%s&q=%s" % (to_langage, langage, to_translate.replace(" ", "+"))
request = urllib2.Request(link, headers=agents)
page = urllib2.urlopen(request).read()
result = page[page.find(before_trans) + len(before_trans):]
result = result.split("<")[0]
return result
if __name__ == '__main__':
short_name = raw_input('Enter short name: ')
short_name_80 = raw_input('Enter short name 80 characters: ')
description = raw_input('Enter description: ')
with open("translate.csv", "w", "utf-8") as f:
for lang in langs:
tmp = lang + ';' + \
translate(short_name, lang) + ';' + \
translate(short_name_80, lang) + ';' + \
translate(description, lang) + '\n'
f.write(tmp)