from urllib import request, parse, error import json
baseURL = 'https://fanyi.baidu.com/sug'
try: word = input('INPUT:') dat = parse.urlencode({"kw": word}).encode() with request.urlopen(baseURL, data=dat) as f: # print('Status:', f.status, f.reason) ret = f.read().decode() ret = json.loads(ret) # print(ret['data']) ls = [a['k'] for a in ret['data']] if word in ls: print('%s: %s' % (word, ret['data'][0]['v'])) print('Similar expressions:') else: print('NOT FOUND. Did you mean:') print(ls) except error.URLError as e: print(e) # 爽!
# Upd on 8.11 import requests import json
BaseURL = 'https://fanyi.baidu.com/sug'
try: word = input('INPUT:') r = requests.post(BaseURL, data={'kw': word}) r.raise_for_status() js = json.loads(r.text) print(js) except requests.RequestException as e: print(e)