python练习 requests有道查询

import requests
import json
while True:
    word = input('请输入要翻译的词语(Q键退出)')
    if word == 'Q':
        break
    else:
        data = {}
        data['i'] = word
        data['from'] = 'AUTO'
        data['to'] = 'AUTO'
        data['smartresult'] = 'dict'
        data['client'] = 'fanyideskweb'
        data['salt'] = '1515466674196'
        data['sign'] = '76449df04b8ec747f663554da8e1cb3d'
        data['doctype'] = 'json'
        data['version'] = '2.1'
        data['keyfrom'] = 'fanyi.web'
        data['action'] = 'FY_BY_REALTIME'
        r = requests.post('http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule', data=data)
        content = json.loads(r.text)  # 将字符串转换为json字典数据  
        print(content['translateResult'][0][0]['tgt'])

你可能感兴趣的:(python练习 requests有道查询)