程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python Google Translate HTML get results

編輯:Python

List of articles

    • 1. Install Translation Library
    • 2. Read file data
    • 3. Operation steps
    • 4. Author Q & A

1. Install Translation Library

Switch to python.exe In the directory .

python -m pip install googletrans==4.0.0-rc1 -i https://mirrors.aliyun.com/pypi/simple/

2. Read file data

See blog https://plugin.blog.csdn.net/article/details/124514912

3. Operation steps

take Html Copy in txt file ,Python Reading data , Call Google translation engine , Finally save to file . Note that the string length cannot exceed 5000 word .

from googletrans import Translator
# Set up Google Translation service address 
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):
# Open a file 
fo = open(filename, "w");
fo.write(data);
# Close open files 
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. Author Q & A

If you have any questions , Please leave a message .


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved