I'm trying to use google translate from a python script:
#!/usr/bin/env python
from urllib2 import urlopen
from urllib import urlencode
base_url = "http://www.google.com/translate?"
params = (('langpair','en|es'), ('u','http://asdf.com'),)
url = base_url+urlencode(params)
print "Encoded URL: %s" % url
print urlopen(url).read()
I'm getting the error 403 when I use it.
# ./1.py
Encoded URL: http://www.google.com/translate?langpair=en%7Ces&u=http%3A%2F%2Fasdf.com
Traceback (most recent call last):
...
urllib2.HTTPError: HTTP Error 403: Forbidden
However, the same URL works fine when accessed from browser. Could anyone spot the error? Or is it that google does not allow this type of usage?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With