-
How to Send a word to translation site from within a Python script?
over 8 years ago
-
about 8 years ago
I didn't understand what do you mean by sending a word to translation site. To me, it makes more sense to call an API to translate your text. But if you really want to use some site, it can be a little tricky. On the top of my head, I can think of two approaches.
1. You have to manually inspect the headers and parameters in the actual request which the site of your choice is using to translate the text and then you have to simulate the same request using curl or requests module with your text and source and target languages. Then parse the response returned and extract the translated text from it.
2. You can use any automation library which would open the site in browser instance, fill the text, language pair with your values and simulate the form submit. Once response gets rendered on browser screen, use any HTML parser and extract your translated text from it.OR,
You can use the simplest approach of using some translation web service.
Some good translation services are:
Paid: [Microsoft Translate][1], [Google Translate][2], Bing Translate
Free: [transltr][3], [hablaa][4]An example using transltr :
import requests import urllib text = 'Test' source_lang = 'en' target_lang = 'es' url = 'http://www.transltr.org/api/translate?text=' + urllib.quote_plus(text) + '&to=' + target_lang + '&from=' + source_lang print url response = requests.get(url) print response.json()
Ofcourse, you need to put checks in this snippet.
https://www.microsoft.com/en-us/translator/default.aspx
https://translate.google.com/
http://www.transltr.org/Developers#!/Language/
http://hablaa.com/api/ -
1 Answer(s)