简体   繁体   English

如何使用谷歌翻译器 API 翻译抓取的文本

[英]how to translate scraped text using google translator API

I scraped a book off Project Gutenberg using beautiful soup in jupyter notebook, and want to translate it into another language.我使用 jupyter notebook 中的美丽汤从古腾堡计划中刮了一本书,并想将其翻译成另一种语言。 However, had trouble doing so.但是,这样做有困难。

Would be grateful for help/advice;将不胜感激帮助/建议; my code so far is below.The translation code did not work, and returned the following error "WriteError: [Errno 32] Broken pipe"到目前为止我的代码如下。翻译代码不起作用,并返回以下错误“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)

The translator.translate(text,dest="ar") method does not return a string, rather instead returns an instance. translator.translate(text,dest="ar")方法不返回字符串,而是返回一个实例。

Try the following snippet:尝试以下代码段:

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

For more information. 了解更多信息。

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

相关问题 如何用谷歌翻译器翻译单词? - how to translate word with google translator? 如何在 Python 中使用谷歌云翻译 API 翻译大文本? - How to translate large text using Google Cloud Translation API in Python? 使用谷歌翻译 API 翻译 python(烧瓶)中的文本 - translate text in python (flask) using google translate API 使用Python使用Google转换的网页数据中缺少信息 - Missing information in scraped web data, Google translate, Using Python 语言翻译器使用谷歌 API in Python - Language Translator Using Google API in Python 如何使用谷歌翻译API的Python来翻译整个txt文件? - How to translate entire .txt file using Google Translate API in Python? 导出到 .csv 或 .xslx 时如何翻译抓取文本的语言? - How to translate language of scraped text when exporting to .csv or .xslx? 在Python中使用Google Translate API,您如何指示某些文本不被翻译? - Using Google Translate API in Python, how do you indicate that some text is not to be translated? 谷歌翻译器 django moduleNotFoundError:没有名为“translate”的模块 - google translator django moduleNotFoundError: No module named 'translate' 使用 Azure 翻译器将文本从日语翻译成英语,但失败并出现错误“400003” - Using Azure translator to translate text from Japanese to English but failing with error '400003'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM