Many do not say, on the code!!!
#!/usr/bin/env python
#coding: Utf-8
ImportUrllib,re,json
defInput_content (URL):#receive aURL (translationAIP)
whileTrue:
Content =Raw_input('Please enter the translated content:')#What the user enters
ifStr. strip (content) = ="':#If the user entered the content is empty
Continue#into the next round of circulation
elifContent = =' Q 'orContent = =' Q ':#if the input isQorQthen quit the program
Exit()
Print'\033[31mbeing translated......\033[0m]
URL = url+content#will beURLstring stitching with translated content
returnUrl#back to the end of the stitchingURL
defHTML (data):#receive the data after the translation is completed
forIinchData
RE = Re.compile (R ' translation.*?\] ')#Define matching rules for regular expressions
List_lost = Re.findall (re,i)#match the content of the translation
Lost = list_lost[0].split (': ')[1]#Remove the match to the content
result = Json.loads (lost)#Deserialize the extracted content
Printresult[0]+'\ n'#Print the results of the translation
if__name__ = =' __main__ ':
Print'\033[33minputQorQExit Translation\033[0m\ n'#Highlight Exit information
whileTrue:
Try:
URL =' http://fanyi.youdao.com/openapi.do?keyfrom=huyuanpy&key=902574943&type=data&doctype=json& Version=1.1&q= '
#Call the YoudaoAPIinterface
Address = input_content (URL)#receive post-stitching content
data = Urllib.urlopen (address)#Pass data to Youdao for translation and get the result after translation
HTML (data)#format the resulting data
except:
Print'\033[31mExiting script......\033[0m\ n'
Exit()
This article is from the "Automated Operations" blog, please be sure to keep this source http://hongchen99.blog.51cto.com/12534281/1908047
Python Development Translation Program