Call Baidu API (2)-Baidu Translation

sequence

Following yesterday's application to call Baidu Translation API (1) , today I will share with you a simple application example. The application involves: python GUI user interface design, method of accessing the server , md5 algorithm encryption and calling Baidu translation API

1. Preliminary knowledge

1. Complete the developer application

Complete the developer application, obtain the appid and key, and activate the service for free (see my last article for details- Application to call Baidu Translation API (1) )

2. Relevant HTTP protocol knowledge

Insert picture description here
Insert picture description here
3. Use of urllib library

Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
4. Send the request

Insert picture description here
5. Refer to official technical documents:

https://api.fanyi.baidu.com/api/trans/product/apidoc

2. Operation example

In the above official documents, find the routine in the figure below
Insert picture description here
. Find the sample in the technical document. After modifying the appid and key, you can complete the call.
Insert picture description here

Three, application

1. Design goals

Design a user window as shown in the figure below to call Baidu Translate's API to realize the translation function
Insert picture description here

2. Brief description

The application involves content including: python GUI user interface design, method of accessing the server, md5 algorithm encryption and calling Baidu translation API

3. Program analysis

(1) Call the required library
Insert picture description here
(2) Design the gui
Insert picture description here
(3) Write the specific content of the two button events
Insert picture description here
Insert picture description here
(4) Call the translation function of Baidu Translation API
Insert picture description here

4. Complete code

from tkinter import *

from urllib import request
from urllib import parse
import json
import hashlib

def translate_word (src_str):
    #simulation browse load host url,get cookie
    URL = "https://api.fanyi.baidu.com/api/trans/vip/translate"
    Form_data = {
    
    'from':'auto','to':'zh','q':src_str,'appid':'填你自己的','salt':'1234567890'}  #创建Form_data字典,存储向服务器发送的data,其中,salt为随机数(任意写)
    Key = '填你自己的'
    m = Form_data['appid'] + src_str + Form_data['salt'] + Key
    m_MD5 = hashlib.md5(m.encode('utf-8'))
    Form_data['sign'] = m_MD5.hexdigest()
    data = parse.urlencode(Form_data).encode('utf-8') #拼接完整请求:http://api.fanyi.baidu.com/api/trans/vip/translate?q=apple&from=en&to=zh&appid=2015063000000001&salt=1435660288&sign=f89f9594663708c1605f3d736d01d2d4

    #或者用这种方式发送请求
    # data ='?appid=' + Form_data['appid'] + '&q=' + parse.quote(Form_data['q']) + '&from=' + Form_data['from'] + '&to=' + Form_data['to'] + '&salt=' + Form_data['salt'] + '&sign=' + Form_data['sign']
    # response = request.urlopen(data)#发送请求,并将得到的响应放入response

    response = request.urlopen(URL,data)     #发送请求,并将得到的响应放入response
    html = response.read().decode('utf-8')   #对网页内容读取并进行解码(从网页控制台源码中可以看到网页的编码方式)
    translate_results = json.loads(html)     #得到的是json类型数据
    print("translate_results(json数据):",translate_results)
    translate_results = translate_results['trans_result'][0]['dst']   #提取json数据中的翻译结果
    print('翻译结果为:',translate_results)
    return translate_results

def leftClick1(event):     #翻译按钮的具体功能
    src_str = Entry1.get()    #得到用户输入的需要翻译的内容
    print('src_str:',src_str)
    vText = translate_word(src_str)
    Entry2.config(Entry2,text = vText)
    Entry2.insert(0,str(vText))

def leftClick2(event):     #清空按钮的具体功能
    Entry1.delete(0,END)   #END必须大写
    Entry2.delete(0,END)

#GUI设计部分:
if __name__ == "__main__":
    root = Tk()
    root.title("单词翻译器")
    root["width"] = 250
    root["height"] = 130

    Label(root,text = "输入要翻译的内容:",width = 15).place(x = 1,y = 1)     #绝对坐标(1,1)
    Entry1 = Entry(root,width = 20)   #添加一个文本框
    Entry1.place(x = 110,y = 1)

    Label(root,text = "翻译的结果:",width = 18).place(x = 1,y = 20)     #绝对坐标(1,1)
    Entry2 = Entry(root,width = 20)  #添加一个文本框
    Entry2.place(x = 110,y = 20)

    Button1 = Button(root,text = "翻译",width = 8)
    Button1.place(x = 40,y = 80)

    Button2 = Button(root,text = "清空",width = 8)
    Button2.place(x = 110,y = 80)

    Button1.bind("<Button-1>",leftClick1)  #将按键和事件绑定
    Button2.bind("<Button-1>",leftClick2)

    root.mainloop()

Four, finally

There will be a share about calling Baidu voice api later, but that one is still under study, the author has figured it out, and will share it with everyone as soon as possible

For the calls of other Baidu API series, please refer to https://blog.csdn.net/weixin_45386875/article/details/113705329

Guess you like

Origin blog.csdn.net/weixin_45386875/article/details/113318707