我的代码就像这样
def translate(self, to_translate, to_langage="auto", langage="auto"):
'''Return the translation using google translate
you must shortcut the langage you define (French = fr, English = en, Spanish = es, etc...)
if you don't define anything it will detect it or use english by default
Example:
print(translate("salut tu vas bien?", "en"))
hello you alright?'''
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, urllib.quote(to_translate.encode('utf8')))
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
我有这本词典
user_all = {1:{0:[],'你最喜欢的5首歌':[u'lethe',u'suliman',u'converting vegetarians',u'invasion',u'betrayel'],'你最喜欢的2位作家':[u'ajdar',u'bulent ersoy'],'Facebook账户链接':[''],'你最喜欢的3部电影':[u'hobbit',u'hobbit 2',你'hobbit 3'],'你最喜欢的3个歌手':[u'infected蘑菇',u'shiro sagisu',u'disturbed'],'Timestamp':[''],'Age':[''], '爱好':[你正在',你骑马','狩猎'','你最喜欢的3个演员/女演员':[u'gerard butler',u'tom hanks',u'jonny deep' ],'选择你最喜欢的3种音乐类型':[u'Blues'],'你最喜欢的运动队':[''],'性别':[u'male'],'Hometown':[u'istanbul' ],'部门':[u'EEE'],'Twitter账户链接':[''],'你的Sehir学生地址':[u'talhatemiz@std.segir.edu.tr'],'你最喜欢的3 Sport Branches':['','',''],'选择你喜爱的2本书类型':[u'Fantasy,短篇小说集合\ xa0'],'你最喜欢的2位导演':[u'guy riche' ,u'david o runnell']},2:{0:[],'你的最喜欢的5首歌曲:'u'Belief',你在燃烧的房间里跳舞',u'seni cok istiyorum',你多喝一杯咖啡',你是简单的男人','你最喜欢的2作家' :[你是。 K. Rowling','u'Sabahattin Ali'],'Facebook账户链接':[u'dgsdg'],'你最喜欢的3部电影':[u'Hobbit',你是指环王',u'araba '','你最喜欢的3个歌手':[u'John Mayer',u'Yavuz Cetin',u'Led Zeppelin'],'时间戳':[''],'年龄':[''],'爱好':[u'Playing Guitar',u'Reading book',u'futbol oynamak'],'你最喜欢的3位演员/女演员':[u'Johnny Depp',u'Natalie Portman',u'Mila Kunis'] ,'选择你最喜欢的3种音乐类型':[u'Blues,Indie,Rock'],'你最喜爱的运动队':[u'dsgg'],'性别':[u'Male'],'Hometown': [u'Artvin'],'部门':[u'EEE'],'Twitter账号链接':[你的'sgsg'],'你的Sehir学生地址':[u'Burak Altun'],'你最喜欢的3 Sport Branches':[u'asd',u'asd',u'as \ u015ffkndl'],'选择你最喜欢的2本书类型':[u'Action and Adventure,Science Fiction'],'Your Favorite 2 Directors' :[u'Steven Spielberg',u'Quentin Tarantino']}}
我对此进行编码以达到每个单词
for i in user_all.values():
for j in i.values():
for m in j:
self.translate(m, to_langage="en", langage="auto")
我收到此错误
追踪(最近一次呼叫最后一次):
文件“/Users/baltun/Documents/workspace/SimSurve/src/Reader/trying_translate.py”,第99行, print read.start_reading()
文件“/Users/baltun/Documents/workspace/SimSurve/src/Reader/trying_translate.py”,第92行,在start_reading中 self.translate(m,to_langage =“en”,langage =“auto”)
文件“/Users/baltun/Documents/workspace/SimSurve/src/Reader/trying_translate.py”,第38行,在翻译中 page = urllib2.urlopen(request).read()
文件“/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/urllib2.py”,第126行,在urlopen中 return _opener.open(url,data,timeout)
文件“/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/urllib2.py”,第397行,公开 response = meth(req,response)
文件“/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/urllib2.py”,第510行,http_response 'http',请求,响应,代码,消息,hdrs)
文件“/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/urllib2.py”,第435行,出错 return self._call_chain(* args)
文件“/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/urllib2.py”,第369行,在_call_chain中 result = func(* args)
文件“/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/urllib2.py”,第518行,http_error_default 提出HTTPError(req.get_full_url(),code,msg,hdrs,fp) urllib2.HTTPError:HTTP错误400:错误请求
你能帮助我吗?
答案 0 :(得分:1)
# coding: utf8
import urllib
import urllib2
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)"}
to_langage, langage, to_translate = 'en', 'fa', u'سلام بر جهان'
before_trans = 'class="t0">'
link = "http://translate.google.com/m?hl=%s&sl=%s&q=%s" % (to_langage, langage, urllib.quote(to_translate.encode('utf8')))
request = urllib2.Request(link, headers=agents)
page = urllib2.urlopen(request).read()
result = page[page.find(before_trans) + len(before_trans):]
result = result.split("<")[0]
print(result)
Hello World
好的,这个例子有效,但我发现为什么你有400个错误。
在user_all
定义的词典中,有一个字符串u'Yavuz Cetin'
,其中包含Yavu
。
在土耳其语中,单词Yavu
表示slow
,因此该单词的谷歌翻译会返回HTTP 400 ERROR !!
要解决此问题,您可能需要在user_all
定义的词典中更改此单词或联系Google!
查看translate.google.com/m?hl=en&sl=auto&tl=en&ie=UTF-8&prev=_m&q=yavu和google.com/search?q=google%20translate%20yavu%20400