前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python3 调用百度翻译API翻译英文

python3 调用百度翻译API翻译英文

作者头像
py3study
发布2020-01-12 20:58:33
1.2K0
发布2020-01-12 20:58:33
举报
文章被收录于专栏:python3python3

自行申请百度开发者账号

代码语言:javascript
复制
import importlib,sys,urllib
importlib.reload(sys)
import urllib.request
  
import json                                                                     #导入json模块  
                                                                           
import hashlib

import urllib
import random




  
def translate(inputFile, outputFile):  
    fin = open(inputFile, 'r',encoding='utf-8')                                              #以读的方式打开输入文件  
    fout = open(outputFile, 'w',encoding='utf-8')                                            #以写的方式代开输出文件  
    appid = '20170307000041649'
    secretKey = 'JcXq9a9QwvxN2l6AhIqH'

 

    myurl = 'http://api.fanyi.baidu.com/api/trans/vip/translate'
    q = 'apple'
    fromLang = 'en'
    toLang = 'zh'
    salt = random.randint(32768, 65536)

   
    
      
    for eachLine in fin:                                                    #按行读入文件  
        line = eachLine.strip()                                         #去除每行首尾可能的空格等  
        if line:
          if line[0].isdigit():
              fout.write(line+"\n")
          else: 
            
              sign = appid+line+str(salt)+secretKey
              sign = hashlib.md5(sign.encode()).hexdigest()
      
              myurl = myurl+'?appid='+appid+'&q='+urllib.parse.quote(line)+'&from='+fromLang+'&to='+toLang+'&salt='+str(salt)+'&sign='+sign
              resultPage = urllib.request.urlopen(myurl)                               #调用百度翻译API进行批量翻译  
       
              print (myurl)
  
              resultJason = resultPage.read().decode('utf-8')                #取得翻译的结果,翻译的结果是json格式
              resultJasons = resultPage.read() 
              print (resultJason)
        
              try:
               js = json.loads(resultJason)                           #将json格式的结果转换成Python的字典结构  
              
  
         
               print ('dst')
               dst = str(js["trans_result"][0]["dst"])                     #取得翻译后的文本结果  
               outStr = dst  
               print (dst)
               if dst[0]
               outDst=dst.strip()+"\n"
               fout.write(outDst)                                     #如果翻译出错,则输出原来的文本  
              except Exception as e:
               fout.write("\n")
               continue
        else:
         
          fout.write("\n")

    
  
        #fout.write(dst.strip().encode('utf-8'))              #将结果输出  
        
    fin.close()  
    fout.close()  
  
if __name__ == '__main__':  
    translate(sys.argv[1], sys.argv[2])                                   #通过获得命令行参数获得输入输出文件名来执行,方便  
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-08-14 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档