簡體   English   中英

如何用谷歌翻譯器翻譯單詞?

[英]how to translate word with google translator?

我正在制作簡單的腳本,使用請求和 BeatifulSoup 將單詞從英語翻譯成俄語,問題是結果框是空的,應該翻譯單詞/我不確定我應該使用 GET 還是 POST 方法。 這是我嘗試過的

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

您可能需要考慮使用googletrans包。

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

這個問題是兩年前提出的,所以我會在這里發布一個答案或者更確切地說是一個建議。 如果deep_translator包符合您的需要,您可能想嘗試它。

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

在這里查看完整的 googletrans 代碼:

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

這是我做的,如果您有圖書館問題,請按以下方式處理

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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM