写一个百度翻译的接口调用模块
时间: 2024-06-10 15:07:29 浏览: 17
好的,以下是一个示例的百度翻译的接口调用模块:
```
import requests
import hashlib
import json
def translate(text, from_lang='auto', to_lang='zh'):
url = 'http://api.fanyi.baidu.com/api/trans/vip/translate'
appid = 'your_app_id' # 替换为自己的百度翻译 API AppID
secret_key = 'your_secret_key' # 替换为自己的百度翻译 API 密钥
salt = str(time.time())[:10]
sign_str = appid + text + salt + secret_key
sign = hashlib.md5(sign_str.encode()).hexdigest()
# 发送请求
data = {
'q': text,
'from': from_lang,
'to':to_lang,
'appid': appid,
'salt': salt,
'sign': sign
}
res = requests.post(url, data=data)
res_dict = json.loads(res.text)
return res_dict['trans_result'][0]['dst']
```
请注意替换以上代码中的 `your_app_id` 和 `your_secret_key` 为你的百度翻译 API 的真实信息。
还有什么我可以帮助你的吗?
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)