[英]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.