[英]how to translate scraped text using google translator API
我使用 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)
translator.translate(text,dest="ar")
方法不返回字符串,而是返回一个实例。
尝试以下代码段:
translation = translator.translate(text,dest="ar")
print(translation.text)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.