Python script to translate through google translate

I am trying to learn python, so I decided to write a script that could translate something using google translate. So far I have written this:

import sys from BeautifulSoup import BeautifulSoup import urllib2 import urllib data = {'sl':'en','tl':'it','text':'word'} request = urllib2.Request('http://www.translate.google.com', urllib.urlencode(data)) request.add_header('User-Agent', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; it; rv:1.8.1.11) Gecko/20071127 Firefox/2.0.0.11') opener = urllib2.build_opener() feeddata = opener.open(request).read() #print feeddata soup = BeautifulSoup(feeddata) print soup.find('span', id="result_box") print request.get_method() 

And now I'm stuck. I do not see any errors in it, but it still does not work (I mean, the script will be executed, but it will not translate the word).

Does anyone know how to fix this? (Sorry for my bad english)

+2
python urllib2 beautifulsoup google-translate
Feb 22 2018-12-22T00:
source share
3 answers

Google translate is for use with a GET request, not a POST request. However, urrllib2 will automatically send POST if you add any data to your request.

The solution is to build a url asking you to GET .
You will need to change the line request = urllib2.Request('http://www.translate.google.com', urllib.urlencode(data)) your code.

Here:

 querystring = urllib.urlencode(data) request = urllib2.Request('http://www.translate.google.com' + '?' + querystring ) 

And you will get the following result:

 <span id="result_box" class="short_text"> <span title="word" onmouseover="this.style.backgroundColor='#ebeff9'" onmouseout="this.style.backgroundColor='#fff'"> parola </span> </span> 

By the way, youโ€™re violating Googleโ€™s terms of service; study them if you do more than crack a little script for training.

Using requests

I highly recommend that you stay away from urllib, if possible, and use the excellent requests library, which will allow you to use HTTP effectively with Python.

+5
Feb 22 '12 at 23:19
source share

I made this script if you want to check it: https://github.com/mouuff/Google-Translate-API :)

+8
Oct 13 '12 at 18:27
source share

Yes, their documentation is not easy to disclose.

Here is what you do:




  1. In the machine where you want to start the client :

    pip install --upgrade google-api-python-client




    1. Then you can write this to send translation requests and get answers:

Here is the code :

 import json from apiclient.discovery import build query='this is a test to translate english to spanish' target_language = 'es' service = build('translate','v2',developerKey='INSERT_YOUR_APP_API_KEY_HERE') collection = service.translations() request = collection.list(q=query, target=target_language) response = request.execute() response_json = json.dumps(response) ascii_translation = ((response['translations'][0])['translatedText']).encode('utf-8').decode('ascii', 'ignore') utf_translation = ((response['translations'][0])['translatedText']).encode('utf-8') print response print ascii_translation print utf_translation 
+1
Jan 17 '17 at 14:22
source share



All Articles