简体   繁体   English

如何用谷歌翻译器翻译单词?

[英]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.我正在制作简单的脚本,使用请求和 BeatifulSoup 将单词从英语翻译成俄语,问题是结果框是空的,应该翻译单词/我不确定我应该使用 GET 还是 POST 方法。 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.您可能需要考虑使用googletrans包。

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.如果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()

see this complete googletrans code over here:在这里查看完整的 googletrans 代码:

https://neculaifantanaru.com/en/python-code-text-google-translate-website-translation-beautifulsoup-library.html 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用谷歌翻译器 API 翻译抓取的文本 - how to translate scraped text using google translator API 如何使用Google翻译将乌尔都语单词音译为罗马乌尔都语 - How to transliterate Urdu word to Roman Urdu Phonetically using Google Translator 谷歌翻译器 django moduleNotFoundError:没有名为“translate”的模块 - google translator django moduleNotFoundError: No module named 'translate' Python翻译,如何只替换一个单词 - Python translator, how to replace just one word 如何在我的 python 代码中正确集成谷歌翻译器 api? - How integrate in my python code the google translator api correctly? 数字到单词翻译器占零 - Accounting for zero in number to word translator 我正在尝试使用谷歌翻译器并在我选择 output 语言英语或印地语时发生翻译,但当 select odia - i am trying to using google translator and translate it is happening when i choosing the output language English or Hindi but when select odia Python 中的 Microsoft Translator 翻译整个 JSON 文件 - Microsoft Translator in Python to translate entire JSON file 如何用python将词从任何一种语言翻译成任何一种语言? - How to translate word to word with python from any language to any language? 如何使用谷歌翻译API的Python来翻译整个txt文件? - How to translate entire .txt file using Google Translate API in Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM