关于有道翻译

import requests
import json
# 1. url
url = 'http://fanyi.baidu.com/sug'
# 2. form/ data/ body
def translate(kw):
form = {
'kw': kw
}
# # 2.1 字典转换成str
# form_str = parse.urlencode(form)
# #print(form_str)
# # 2.2 str转换成bytes
# form_bytes = form_str.encode('utf-8')
# # 3. 调用函数 url form
# response = request.urlopen(url, data=form_bytes)

# request 的post方法色使用
response = requests.post(url, data=form)

# str = response.content.decode('utf-8/gbk/gb2312/gb18080') =
response.encoding = 'utf-8/gbk/gb2312/gb18080'
str = response.text

# 4. 解析json
res_dict = json.loads(response.text)
# 5. 打印翻译结果
#print(res_dict)
result = res_dict['data'][0]['v']
return result

if __name__ == '__main__':
res = translate('哈哈')
print(res)

转载于:https://www.cnblogs.com/wopuchezhan/p/9526473.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值