在使用下面的代码,我没有得到准确的格式,因为我得到之前翻译。请指导我哪里我犯了错误。
代码
from googletrans import Translator
from bs4 import BeautifulSoup, Tag,NavigableString
html = open("data.html").read()
soup = BeautifulSoup(html)
tags = soup.find_all(["p","ul","ol","h1","h2","h3","h4","h5","h6","td"])
translator=Translator()
def callcon(t, tag):
for i in range(0, len(t)):
if type(t[i])== NavigableString:
#print(t[i])
translation=translator.translate(t[i],dest="hi").text
new_text = tag.find(text=str(t[i])).replace_with(translation)
#print(new_text)
#og=og.replace(str(t[i]),"suneri")
#return og
else:
#print(t[i].contents)
#print(og)
callcon(t[i].contents,tag)
for tag in tags:
#constr="".join(str(e) for e in tag.contents)
#print(constr)
callcon(tag.contents, tag)
with open("output2.html", "wb") as f_output:
f_output.write(soup.prettify("utf-8"))
1条答案
按热度按时间rdlzhqv91#