您当前的位置: 首页 >  Python

插件开发

暂无认证

  • 5浏览

    0关注

    492博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

Python-谷歌翻译-Html-获取结果

插件开发 发布时间:2022-06-25 10:15:00 ,浏览量:5

文章目录
    • 1.安装翻译库
    • 2.读取文件数据
    • 3.操作步骤
    • 4.作者答疑

1.安装翻译库

切换到python.exe所在目录。

python -m pip install googletrans==4.0.0-rc1 -i https://mirrors.aliyun.com/pypi/simple/
2.读取文件数据

参阅博文https://plugin.blog.csdn.net/article/details/124514912

3.操作步骤

将Html拷贝入txt文档,Python读取数据,调用谷歌翻译引擎,最后保存到文件。注意字符串长度不能超过5000字。

from googletrans import Translator
# 设置Google翻译服务地址
translator = Translator(service_urls=[
      'translate.google.cn'
])

def ReadFile(filename):
      ff=None;
      with open(filename, 'r') as f:
            ff = f.read();
      return ff;

def WriteFile(filename,data):
      # 打开一个文件
      fo = open(filename, "w");
      fo.write(data);
      # 关闭打开的文件
      fo.close();


htmlStr=ReadFile(r"C:\Users\ajz\Desktop\html.txt");
translation = translator.translate(htmlStr, dest='zh-CN');
print(translation.text);
WriteFile(r"C:\Users\ajz\Desktop\htmlrlt.txt",translation.text);
4.作者答疑

如有疑问,敬请留言。

关注
打赏
1665481431
查看更多评论
立即登录/注册

微信扫码登录

0.0710s