Use Python as a translation software.

Source: Internet
Author: User
Tags urlencode

Python 2.7.13
IDE Pycharm 5.0.3

MacOS 10.12.1 Preface

It took a little time, half copy, half a rewrite, to write the first available Python applet, the role is in the IDE simulation Youdao dictionary access, the effect shown below, the disadvantage is that when the input in the English string over a certain number, will throw in the middle code, novice do not know how to deal with, hope to know the generous enlighten First Order: Interactive interface

First, the interaction in Jupyter or pycharm, the core statement is to use the Raw_input capture system input effect graph

Code

#-*-coding:utf-8-*-import urllib2 Import Urllib # python2.7 requires two urllib import json while true:content = raw_in Put ("Please enter the content to be translated:") # System capture input, is the command box pop-up prompts, you need to manually enter if content = = ' Q ': # input Q exit while loop break URL = "http:// Fanyi.youdao.com/translate?smartresult=dict&smartresult=rule&smartresult=ugc&sessionfrom=null "Data = {} # construct data, inside construct parameter passed data[' type '] = ' AUTO ' data[' i ']=content data[' doctype '] = ' json ' data[' xmlversion '] = ' 1.8 ' data[' keyfrom '] = ' fanyi.web ' data[' UE '] = ' UTF-8 ' data[' action '] = ' fy_by_enter ' ' data[' typores Ult '] = ' true ' data = Urllib.urlencode (data). Encode (' Utf-8 ') # will construct the data encoding req = Urllib2. Request (URL) # requests to the browser response = Urllib2.urlopen (req, data) # with parameter request, return execution result html = Response.read (). Decode (' utf- 8 ') # print (HTML) # You can cancel the print annotation to see the effect, and the result here is to parse target = json.loads (HTML) # Load the fetched HTML string in JSON form the PRI NT "Translation of the content is:" +target[' Translateresult '][0][0][' TGT '].enCode (' Utf-8 ') # Please enter what you want to translate: Test # translation is: Testing # Please enter what you want to translate: Test # The content of the translation is: Testing # Please enter what you want to translate: Q 

Note: The data dictionary here is based on the data in the actual Web page, may be different, the specific operation, click the review element. Or see the Little Turtle 54 speak. Advanced : Made into GUI

From the practical still two steps, the first step is to make the GUI first

I can refer to the previous: Python based on Tkinter two input rules (Beggar version) interface effect

Code

#-*-Coding:utf-8-*-' Author: Husky says Meow udate:2016.7.21 ' ' from tkinter import * Import difflib import urllib2 import u Rllib # python2.7 requires two urllib import JSON #----------------------The main frame part----------------------root = Tk () root.title (' Flip Gui&beta1 ') root.geometry () Label_root=label (root) #-----------------------define rules------------------------def Translate (content): url = "http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule&
    Smartresult=ugc&sessionfrom=null "data = {} # constructs data, inside constructs the parameter to pass data[' type '] = ' AUTO ' data[' i ']=content data[' doctype '] = ' json ' data[' xmlversion '] = ' 1.8 ' data[' keyfrom '] = ' fanyi.web ' data[' UE '] = ' UTF-8 ' d ata[' action '] = ' fy_by_enter ' data[' typoresult ' = ' true ' data = Urllib.urlencode (data). Encode (' Utf-8 ') # will construct the DA Ta code req = urllib2. Request (URL) # requests to the browser response = Urllib2.urlopen (req, data) # with parameter request, return execution result html = Response.read (). Decode (' utf- 8 ') # Print (HTML) # you can cancel the print annotation to see the effect, and the result here is to parse target = json.loads (HTML) # load the obtained HTML string in JSON #print u "The translation is:" +target[ ' Translateresult '][0][0][' TGT '] return to target[' Translateresult '][0][0][' TGT '].encode (' utf-8 ') #还可以继续增加规则函数, As long as the two input parameters can be #----------------------trigger function-----------------------def ANSW (): # rule function Ans.insert (end, "translation%s:"%var_first . get (). Encode (' utf-8 ') + Translate (Var_first.get (). Encode (' Utf-8 ')) def Clea (): #清空函数 input_num_first.delete (0,end) #这里entry的delect用0 Ans.delete (0,end) #text中的用0.0 #----------------------Input selection Framework--------------------Frame_input = Fram E (Root) Label_input=label (frame_input, text= ' Please enter what needs to be translated ', (') ') Var_first = Stringvar () Input_num_first = Entry
(Frame_input, Textvariable=var_first) #---------------------calculation result frame---------------------Frame_output = frame (root)


Label_output=label (frame_output) Ans = ListBox (Frame_output, height=5,width=30) #text也可以, the listbox benefits from wrapping #-----------------------Button-----------------------Calc = button (frame_output,text= ' translation ', COMMAND=ANSW) CLE = button (frame_output,text= ' empty ', Command=clea) label_r Oot.pack (side=top) frame_input.pack (side=top) label_input.pack (side=left) input_num_first.pack (Side=LEFT) frame_ Output.pack (side=top) label_output.pack (side=left) calc.pack (side=left) cle.pack (side=left) Ans.pack (Side=LEFT) #-- -----------------Root.mainloop ()------------------Root.mainloop ()
High -Order: Publishing Applications

How to install B in front of a small partner is my motivation to study, hahaha
Tutorial updates are as follows: Package your own Python programs into. Exe/.app (show classmate a face na) Pay Attention

Python3 users notice that the use of the URL package and Python2 is different, please according to the actual needs of Baidu

Python If the operation is too fast or the Web page restricts access to the robot, you need to modify the head, after modifying the code. Of course, each computer's user is different, specifically to review the element view.

req = Urllib2. Request (URL) # Generate Object
# Add the following line of code;
req.add_header (' user-agent ', ' mozilla/5.0 (Windows NT 10.0; WOW64) applewebkit/537.36 (khtml, like Gecko) chrome/45.0.2454.101 safari/537.36 '), so that it can be disguised as human beings
Of course, you can also add a delay module to restrict access time.
Import Time  #添加延时模块
time.sleep (1) #休息1秒钟再进行操作

Python3 students need to tkinter to lowercase, there is the conversion of attention to the encoding part, specific suggestions can be seen Python based on the Tkinter two input rules (Beggar version) of the reference recommendations

Mac students may encounter Tkinter can not enter the Chinese issue, may be caused by the Tkinter version too low, solution reference: MAC system, Tkinter can not use Chinese Input method to enter Chinese update 2016.6.14 first written 2017.7.22 Re-typesetting, adding tkinter, etc.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.