The code on GitHub has changed slightly.
1 Importurllib.request2 ImportUrllib.parse3 ImportJSON4 ImportOS5 6 7 while(1): 8URL = r'http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule&smartresult=ugc& sessionfrom=http://www.youdao.com/'9 Ten " " One head={} A head[' user-agent ' = ' mozilla/5.0 (Windows NT 6.1; WOW64) applewebkit/537.36 (khtml, like Gecko) chrome/35.0.1916.153 safari/537.36 ' - " " -Content = input ("input the next original text:") theData={} - -data['type'] ='AUTO' -data['I'] =content +data['DOCTYPE'] ='JSON' -data['xmlversion'] ='1.6' +data['Keyfrom'] ='Fanyi.web' Adata['UE'] ='UTF-8' atdata['Typoresult'] ='true' - -data = Urllib.parse.urlencode (data). Encode ('Utf-8') - -Reponse =Urllib.request.urlopen (url,data) -html = Reponse.read (). Decode ('Utf-8') in -target =json.loads (HTML) toOs.system ('CLS') + Print("Original text:%s"%content) - Print("Translation:%s"% (target['Translateresult'][0][0]['TGT'])) the Print('\ n') * Print('\ n') $ Panax Notoginseng
I don't know if this guy was original.
Link http://www.cnblogs.com/90zeng/p/python_translation.html?utm_source=tuicool&utm_medium=referral
and change it into requests.
1 ImportRequests2 ImportJSON3 ImportOS4 5 6 while(1): 7URL = r'http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule&smartresult=ugc& sessionfrom=http://www.youdao.com/'8 9 " "Ten head={} One head[' user-agent ' = ' mozilla/5.0 (Windows NT 6.1; WOW64) applewebkit/537.36 (khtml, like Gecko) chrome/35.0.1916.153 safari/537.36 ' A " " -Content = input ("input the next original text:") -Data={} the -data['type'] ='AUTO' -data['I'] =content -data['DOCTYPE'] ='JSON' +data['xmlversion'] ='1.6' -data['Keyfrom'] ='Fanyi.web' +data['UE'] ='UTF-8' Adata['Typoresult'] ='true' at - -reponse = Requests.get (url,params=data) -html = Reponse.content.decode ('Utf-8') - - intarget =json.loads (HTML) -Os.system ('CLS') to Print("Original text:%s"%content) + Print("Translation:%s"% (targe['Smartresult']['Entries'][1:])) - Print('\ n') the Print('\ n')
Then later applied Youdao API with API really convenient QAQ
1 ImportRequests2 ImportJSON3 ImportOS4 5 6 7URL = r'http://fanyi.youdao.com/openapi.do?keyfrom=*******&key=*******&type=data&doctype=json& Version=1.1&q='8 9 TenOtext = input ("input the next original text:") One Print('\ n') A - -reponse = Requests.get (url+otext) thehtml = Reponse.content.decode ('Utf-8') -target =json.loads (HTML) - - +j=0 - if(Target.get ("Basic",'Not found')!='Not found'): + Print(Phonetic:"+ ' [' +target["Basic"]["Phonetic"]+ ') A Print('\ n') at forIinchtarget["Basic"]["explains"]: - if(i): - Print(target["Basic"]["explains"][j]) -J=j+1 - Else: - Print(target["translation"])
Python Youdao Translator