Python Learning Exchange Group: 125240963, the group to share the daily dry, including the latest Python enterprise case study materials and 0 basic introductory tutorials, welcome to the group of small partners to learn exchange
Nowadays, more and more emphasis is on English ability. If you know English, you will be exposed to a larger world and you will find more information. Especially for programmers, good programming materials come from English documents. Those domestic translation of the version, there are a lot of content and the original difference too much, or even irrelevant.
Although there are a lot of translation software now, it is very fulfilling to write a Python applet by myself. Even if you use today's code, you can also develop a small translation software yourself.
Youdao Dictionary Interface
Today's translation process is to borrow the Youdao translation interface to achieve. The logic of the program is very simple, is to use Youdao translation, the need to translate the content as parameters, to the corresponding URL. Then we can get the corresponding translation result by returning a JSON data through the Youdao server.
Program Call Results
Start the program in the terminal input you want to translate the content, can be translated into English, can also be translated into English. such as input "koc most handsome! 」。
The corresponding input in English can also be translated into Chinese.
Program code
The program code is very simple, I have comments in the corresponding code.
1 ImportJSON2 3 ImportRequests4 5 #translation functions, Word needs to translate the content6 defTranslate (word):7 #Youdao Dictionary API8URL ='http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule&smartresult=ugc& Sessionfrom=null'9 #The parameters of the transfer, where I is the content to be translatedTenKey = { One 'type':"AUTO", A 'I': Word, - "DOCTYPE":"JSON", - "version":"2.1", the "Keyfrom":"Fanyi.web", - "UE":"UTF-8", - "Action":"Fy_by_clickbutton", - "Typoresult":"true" + } - #key This dictionary is sent to the Youdao dictionary server content +Response = Requests.post (URL, data=key) A #determine if the server is successful at ifResponse.status_code = = 200: - #then the corresponding results - returnResponse.text - Else: - Print("Youdao Dictionary call failed") - #The corresponding failure will return null in returnNone - to defget_reuslt (repsonse): + #load the returned result into JSON format via Json.loads -result =json.loads (repsonse) the Print("the word entered is:%s"% result['Translateresult'][0][0]['src']) * Print("The translation result is:%s"% result['Translateresult'][0][0]['TGT']) $ Panax Notoginseng defMain (): - Print("This program calls Youdao Dictionary API for translation, can achieve the following effects:") the Print("Foreign Language --Chinese") + Print("Chinese-English") AWord = input ('Please enter the word or sentence you want to translate:') theList_trans =Translate (word) + get_reuslt (List_trans) - $ if __name__=='__main__': $Main ()
Crazy Sea
Links: https://juejin.im/post/5b4bdb81e51d45616f4575dd
50 lines of Python code for easy translation in Chinese and English