Python 有道词典爬虫

import urllib.request
import urllib.parse
import json
import sys
while 1:
    content = input('请输入要翻译的内容:')
    if content == '  ':
        print('程序结束')
        sys.exit()
    url = 'http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule&sessionFrom='

    data = {}
    data["i"]=content
    data["from"]="AUTO"
    data["to"]="AUTO"
    data["smartresult"]="dict"
    data["client"]="fanyideskweb"
    data["salt"]="502865709143"
    data["sign"]="e7b725d55dd02ab7b3a17c44170950ad"
    data["doctype"]="json"
    data["version"]="2.1"
    data["keyfrom"]="fanyi.web"    
    data["action"]="FY_BY_CLlCKBUTTON"
    data["typoResult"]="true"
    data = urllib.parse.urlencode(data).encode('utf-8')

    response = urllib.request.urlopen(url,data)
    html = response.read().decode('utf-8')
    target = json.loads(html)
    print("翻译结果为: %s" % (target['translateResult'][0][0]['tgt']))

 

你可能感兴趣的:(Python)