Python使用百度翻译开发平台实现英文翻译为中文功能示例

yipeiwu_com5年前Python基础

本文实例讲述了Python使用百度翻译开发平台实现英文翻译为中文功能。分享给大家供大家参考,具体如下:

#coding=utf8
import random
import requests
import hashlib
appid = 'xxxxxx'
secretKey = 'xxxxx'
def get_md5(string):#返回字符串md5加密
  hl = hashlib.md5()
  hl.update(string.encode('utf-8'))
  return hl.hexdigest()
def en_to_zh(en_str):#英语翻译成中文
  api_url = 'http://api.fanyi.baidu.com/api/trans/vip/translate'
  salt = random.randint(32768,65536)
  sign = get_md5(appid + en_str + str(salt) + secretKey)
  api_data = {
    'q':en_str,
    'from':'en',
    'to':'zh',
    'appid':appid,
    'salt':salt,
    'sign':sign
  }
  req_get = requests.get(api_url,api_data)
  result = req_get.json()
  return result['trans_result']

更多关于Python相关内容可查看本站专题:《Python Socket编程技巧总结》、《Python数据结构与算法教程》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》、《Python入门与进阶经典教程》及《Python文件与目录操作技巧汇总

希望本文所述对大家Python程序设计有所帮助。

相关文章

python实现自动更换ip的方法

本文实例讲述了python实现自动更换ip的方法。分享给大家供大家参考。具体实现方法如下: #!/usr/bin/env python #-*- encoding:gb2312 -*...

深入浅出分析Python装饰器用法

本文实例讲述了Python装饰器用法。分享给大家供大家参考,具体如下: 用类作为装饰器 示例一 最初代码: class bol(object): def __init__(self...

Python列表list内建函数用法实例分析【insert、remove、index、pop等】

Python列表list内建函数用法实例分析【insert、remove、index、pop等】

本文实例讲述了Python列表list内建函数用法。分享给大家供大家参考,具体如下: #coding=utf8 ''''' 标准类型函数: cmp():进行序列比较的算法规则如下:...

python在Windows下安装setuptools(easy_install工具)步骤详解

python在Windows下安装setuptools(easy_install工具)步骤详解

本文讲述了python在Windows下安装setuptools(easy_install工具)的方法。分享给大家供大家参考,具体如下: 【题外话介绍下setuptools】 setup...

Centos部署django服务nginx+uwsgi的方法

1.安装python3 yum -y install wget gcc make zlib-devel readline-devel bzip2-devel ncurses-dev...