问题
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?
Thanks in advance
回答1:
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
回答2:
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
回答3:
You want to use the offical Google Translate APIs:
http://code.google.com/intl/de-DE/apis/language/translate/overview.html
Apart from that:
http://www.catonmat.net/blog/python-library-for-google-search/
回答4:
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
来源:https://stackoverflow.com/questions/5871710/translate-url-with-google-translate-from-python-script