【问题标题】:how to translate scraped text using google translator API如何使用谷歌翻译 API 翻译抓取的文本
【发布时间】:2021-05-13 08:24:13
【问题描述】:

我用 jupyter notebook 中的美汤从古腾堡计划中刮了一本书,想把它翻译成另一种语言。但是,这样做有困难。

非常感谢您的帮助/建议;到目前为止我的代码如下。翻译代码不起作用,并返回以下错误“WriteError:[Errno 32] Broken pipe”

#Store url

url = 'https://www.gutenberg.org/files/514/514-h/514-h.htm'
html = r.text
print(html)
#Create a BeautifulSoup object from the HTML
soup = BeautifulSoup(html, "html5lib")
type(soup)

#get rid of non-text 

paragraph=soup.find_all("p")
for para in paragraph:
    print(para.text)

#translate text using google API translator
#init the Google API translator

translator = Translator()
translation = translator.translate(text,dest="ar")
print(translation)

【问题讨论】:

    标签: python language-translation


    【解决方案1】:

    translator.translate(text,dest="ar") 方法不返回字符串,而是返回一个实例。

    试试下面的 sn-p:

    translation = translator.translate(text,dest="ar")
    print(translation.text)
    

    For more information.

    【讨论】:

    • 非常感谢!这很有帮助。我设法翻译了一个段落。但是,我怎样才能翻译整个文本呢?文本保存在 for 循环中(见下文)。不知道如何将其转换为变量。对于段落中的段落: print(para.text)
    • new_var = "" for para in paragraph: new_var += para.text print(new_var)
    猜你喜欢
    • 2019-05-20
    • 1970-01-01
    • 2011-12-26
    • 1970-01-01
    • 1970-01-01
    • 2011-06-06
    • 1970-01-01
    • 2017-03-13
    • 2016-11-18
    相关资源
    最近更新 更多