import urllib.request import urllib.parse import json #from tkinter import * '''root =Tk() root.wm_attributes('-topmost',1) root.geometry('+1000+40') root.minsize(300,200)''' fanyi = input('Enter what you want to translate:') url = "http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule&sessionFrom=" #url? There is an _o in front of it. After deleting it, the errorCode=50 in the response will be resolved. head = {} head['User-Agent']='Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.101 Safari/537.36]' data = {} data['i']=fanyi data['from']='AUTO' data ['to'] = 'AUTO' data['smartresult']='dict' data['client']='fanyideskweb' data['salt']='1507343425426' data['sign']='0df813336ad806fbaa2e0120dddb1d02' 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') response = urllib.request.urlopen(url,data) html = response.read().decode('utf-8') print (html) target = json.loads(html) result = target['translateResult'][0][0]['tgt'] print (result)
Previously, errorCode=50 was displayed at print(response), but after Baidu heard that it was a problem with encrypted salt, I searched for the algorithm of salt and sign variables, but it still failed.
After it came to an end, I found out yesterday that the fish C forum thought that netizens had made a tk desktop translation crawler. After checking the source code, I found that I just removed the _o in the url, and it could be used.