how to translate word with google translator?

Question:

I’m making simple script that will translate words from English to Russian language using requests and BeatifulSoup, the problem is that the result box is empty where should be translated word/ I’m not sure if i should use GET or POST method. This is what I’ve tried

with open('File.csv', 'r') as file:
    csv_reader = csv.reader(file)

    for line in csv_reader:
        if line[1] == '':

            url = 'https://translate.google.com/#en/ru/{}'.format(line[0])
            r = requests.get(url, timeout=5)
            soup = BeautifulSoup(r.content, 'html.parser')

            translate = soup.find('span', id='result_box')
            for word in translate:
                print(word.find('span', class_=''))
Asked By: Andrew

||

Answers:

You might want to consider using the googletrans package.

from googletrans import Translator
translator = Translator()
text = translator.translate('text', src='en', dest='ru')
print(text.text)
Answered By: tbienias

The question was asked two years ago so I ll post an answer or rather a suggestion here. You may want to try the deep_translator package if it matches your needs.

from deep_translator import GoogleTranslator

translated = GoogleTranslator(source='auto', target='ru').translate(text='happy coding')
Answered By: basilisk
from bs4 import BeautifulSoup
from bs4.formatter import HTMLFormatter
from googletrans import Translator
import requests

translator = Translator()

see this complete googletrans code over here:

https://neculaifantanaru.com/en/python-code-text-google-translate-website-translation-beautifulsoup-library.html

Answered By: Just Me

this is what i did, if you have library problem please handle it in the following way

cmd: pip3 uninstall googletrans
cmd: pip3 install googletrans==3.1.0a0
from googletrans import Translator
translator = Translator()

text = "How to convert some text to multiple languages"
destination_language = {
    "spanish": "es",
    "chinese": "zh-CN",
    "vietnamese": "vi",
    "korean": "ko",
    "japanese": "ja",
    "french": "fr",
}

for key, value in destination_language.items():
    trans = translator.translate(text, dest=value).text
    print(trans)
Answered By: sonpxp
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.