我的第一个爬虫程序——有道翻译

import urllib.request
import urllib.parse
import json
url = 'http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule' #翻译地址

while True:
    content = input('请输入需要翻译的内容:')
    data = { }
    data['i'] = content  
    data['doctype'] = 'json'  #json编码格式
    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']))

猜你喜欢

转载自blog.csdn.net/lishan132/article/details/102920012