简体   繁体   中英

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. 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"

#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.

Try the following snippet:

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

For more information.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM