translate url with google translate from python script
Question: 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


Answer: 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
    

