translate url with google translate from python script

♀尐吖头ヾ 提交于 2019-12-04 11:52:39

If Google doesn't let you do this, you could programatically translate the normal website's source via the Google's APIs.

I wrote a function for this a little while back:

def translate(text, src = '', to = 'en'):
  parameters = ({'langpair': '{0}|{1}'.format(src, to), 'v': '1.0' })
  translated = ''

  for text in (text[index:index + 4500] for index in range(0, len(text), 4500)):
    parameters['q'] = text
    response = json.loads(urllib.request.urlopen('http://ajax.googleapis.com/ajax/services/language/translate', data = urllib.parse.urlencode(parameters).encode('utf-8')).read().decode('utf-8'))

    try:
      translated += response['responseData']['translatedText']
    except:
      pass

  return translated

You should be using the google API. I found and tested this code, it works:

#!/usr/bin/env python
from urllib2 import urlopen
from urllib import urlencode
import sys

lang1=sys.argv[1] lang2=sys.argv[2] langpair='%s|%s'%(lang1,lang2) text=' '.join(sys.argv[3:]) base_url='http://ajax.googleapis.com/ajax/services/language/translate?' params=urlencode( (('v',1.0), ('q',text), ('langpair',langpair),) ) url=base_url+params content=urlopen(url).read() start_idx=content.find('"translatedText":"')+18 translation=content[start_idx:] end_idx=translation.find('"}, "') translation=translation[:end_idx] print translation

source

your problem is because you have no headers

(which tells google what is your browser and compatibility)

I had this error before when I made my google translate api

you can find it here: https://github.com/mouuff/Google-Translate-API

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!