简体   繁体   中英

how to translate word with google translator?

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_=''))

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)

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')
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

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM