urllib using the library
urllib library is a python standard library of the most commonly used PYthon web access module that allows users to access as a local text file as read the contents of the page.
urlib.request module : used to open and read the url
urlib.error module : contains the urlib.request errors generated, can try capture
urlib.parse module : contains some analytical url methods
urlib.robotparser module : used to parse the robots.txt text file
Case Little translator:
Design ideas:
1. Use Baidu translated to http://api.fanyi.baidu.com/api/trans/vip/translate this url address post transmission method 6 large access service request parameters ---- and generates a signature and signature calculation
2. The format conversion and get json data result format - the signature turn into utf8 and the translation Baidu url with utf8 decoded and read, with json open format translation Baidu url
3. Click translational events: get user input - and translate
Tracking variable values change, and updated values are displayed on screen
4. Empty event
The interface layout design
from tkinter import *
from urllib import request
from urllib import parse
import json
import hashlib
import random
def translate_Word(en_str):
URL = 'http://api.fanyi.baidu.com/api/trans/vip/translate'
# en_str = input ( " Please enter the content to be translated :")
# Create a Form_Data dictionary, stored sent to the server Data
#Form_Data={'from':'en','to':'zh','query':en_str,'transtype':'hash'}
Form_Data = {}
Form_Data['from'] = 'en'
Form_Data['to'] = 'zh'
Form_Data [ 'q'] = en_str # to translate data
Form_Data['transtype'] = 'hash'
Form_Data [ 'appid'] = ' 20151113000005349' # application APP ID
Form_Data [ 'salt'] = str (random.randint (32768, 65536)) # nonce
Key = "osubCEzlGjzvw8qdQc41" # platform distribution key
m=Form_Data['appid']+en_str+Form_Data['salt']+Key
m_MD5 = hashlib.md5(m.encode('utf8'))
Form_Data['sign'] = m_MD5.hexdigest()
data = parse.urlencode (Form_Data) .encode ( 'utf-8') # use urlencode method of converting a standard format
response = request.urlopen (URL, data) # pass Request data objects and format conversion End
html = response.read (). decode ( 'utf-8') # reads and decodes information
translate_results = json.loads(html) #使用JSON
print (translate_results) # print out JSON data
translate_results = translate_results [ 'trans_result'] [0] [ 'dst'] # found translation
print ( " the result of the translation is: % S"% translate_results) # print translation information
return translate_results
def leftClick (event): # translate button function event
#print ( "x -axis coordinate :", event.x)
#print ( "y -axis coordinate :", event.y)
en_str = Entry1.get () # get the content to be translated
print (en_str)
vText = translate_Word (en_str)
# Entry2.config (Entry2, text = vText ) # modify the label text tips
s.set("")
Entry2.insert (0, vText)
def leftClick2 (event): # Clear button event function
s.set("")
# Entry2.config (Entry2, text = vText ) # modify the label text tips
Entry2.insert(0,"")
if __name__ == "__main__":
root = Tk()
root.title ( " word translator ")
root['width']=250;root['height']=130
( 'Root, text = Label contents of the input to be translated: ', width = 15) .place (X = 1, Y = 1) # absolute coordinates ( 1 , 1 )
Entry1=Entry(root,width=20)
Entry1.place (x = 110, y = 1) # absolute coordinates ( 110 , 1 )
Label (root, text = ' translation result: ', width = 18 is) .place (X = 1, Y = 20) # absolute coordinates ( 1 , 20 )
s = StringVar () # a StringVar () Object
s.set("")
Entry2=Entry(root,width=20,textvariable=s)
Entry2.place (x = 110, y = 20) # absolute coordinates ( 110 , 20 )
Button1=Button(root,text = '翻译',width=8)
Button1.place (x = 40, y = 80) # absolute coordinates ( 40 , 80 )
Button2=Button(root,text = '清空',width=8)
Button2.place (x = 110, y = 80) # absolute coordinates ( 110 , 80 )
# To Label bind mouse listener events
Button1.bind ( "<Button-1> ", leftClick) # translate button
Button2.bind ( "<Button-1> ", leftClick2) # clear button
root.mainloop ()