pythonは練習します5:登ります。

6147 ワード

具体的なコードは以下の通りです
import urllib.request
import urllib.parse #     url   ,          
import json #                     

content = input('        : ')
url = 'http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule' 
#  network      url,    _o  ,        ,     csdn    
data = {
     } 
#  network   data,    
data['i'] = content
data['from'] = 'AUTO'
data['to'] = 'AUTO'
data['smartresult'] = 'dict'
data['client'] = 'fanyideskweb'
data['salt'] = '15938760135952'
data['sign'] = 'af3f1c36a96655e1a13616ec4e523d9c'
data['ts'] = '1593876013595'
data['bv'] = 'ce1fe729a12a6b5169046dd3aac11e4c'
data['doctype'] = 'json'
data['version'] = '2.1'
data['keyfrom'] = 'fanyi.web'
data['action'] = 'FY_BY_CLICKBUTTION'
data = urllib.parse.urlencode(data).encode('utf-8')
# urlencode()     URL  ,      ,       。
#           utf-8   

response = urllib.request.urlopen(url, data)
html = response.read().decode('utf-8') #    html    
target = json.loads(html)['translateResult'][0][0]['tgt']
#                         
print('    : ', target)