python 3.7.0 小甲鱼 有道词典翻译 (error:50)问题解决

#一种实现案方案是:

import urllib.request
import urllib.parse
import json

#Request URL
while 1:
    i = input("请输入要翻译的文字:")
    if i=='!q':
        print("退出")
        break

    url = "http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule"
    data = {}
    # 注意head文件!
    head="Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.98 Safari/537.36 LBBROWSER"
    
    # From Data 全部属性   
    data['i'] = "我是中国人!"
    data['from'] = 'AUTO'
    data['to'] = 'AUTO'
    data['smartresult'] = 'dict'
    data['client'] = 'fanyideskweb'
    data['datasalt'] = '1531398987689'
    data['sign'] = 'b2116a1fa17e3c04da517b8e8282fed8'
    data['doctype'] = "json"
    data['version'] = '2.1'
    data['keyfrom'] = 'fanyi.web'
    data['action'] = 'FY_BY_CLICKBUTTION'
    data['typoResult'] = 'true'
    #转码
    data = urllib.parse.urlencode(data).encode("utf-8")
    #打开链接
    req = urllib.request.Request(url,data)
    req.add_header("User-Agent",head)
    response = urllib.request.urlopen(req)

    #转为Unicode
    html = response.read().decode("utf-8")

    #json文件读取
    target = json.loads(html)

    #字典列表输出
    #print(html)
    print(target["translateResult"][0][0]["tgt"])

你可能感兴趣的:(url,模块调用,有道词典翻译,小甲鱼)