利用百度翻译接口,模拟查询英文单词功能
import requests
import hashlib
import json
url = 'https://fanyi-api.baidu.com/api/trans/vip/translate'
# sign:签名,salt:随机数,appid:注册成百度开发者的appid,secret_key:注册成百度开发者的密钥
secret_key = ''
q = 'apple'
salt = ''
appid = ''
# md5加密
sign = hashlib.md5((appid + q + salt + secret_key).encode(encoding='utf-8')).hexdigest()
para = {"q":q,"from":"en","to":"zh","appid":appid,"salt":salt,"sign":sign}
res = requests.get(url,params=para)
# 结果中文是Unicode,需要进行解码
result = res.content.decode('unicode-escape')
# 反序列化为python对象
result = json.loads(result)
print(result)