Python implements a desktop translation tool

Python has been in use for a long time, initially based on interest in reptiles. With the continuous deepening, I gradually changed to other languages, after all, the job opportunities are really too few. Many skills will be forgotten if they are not used for a long time, and they will be sorted out for beginners to learn and discuss. Compared with other languages, you can achieve a complete function with very little code.

ps: in the process of learning python, many other people often give up because they can't solve problems, so I built a python full stack development exchange. Skirt: after a long time of martial arts, I can find it under the conversion of streaming and thinking (Digital homophony). There are the latest Python tutorial projects to take, and the problems that I don't understand can be solved by the old driver, and they can supervise each other and make progress together!

For the convenience of testing, the source code will be posted here:

import requests
import time
import random
import hashlib

def get_md5(string):
    string = string.encode('utf-8')
    md5 = hashlib.md5(string).hexdigest()
    return md5

def translates():
    context = input('Please enter what you want to translate:')
    ts = str(int(time.time()*1000))
    salt = ts + str(random.randint(0, 9))
    bv = get_md5("5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36")
    sign = get_md5("fanyideskweb" + context + salt + "n%A-rKaT5fb[Gy?;N5@Tj")
    data = {
        'i' : context,
        'form' : 'AUTO',
        'to' : 'AUTO',
        'smartresult' : 'dict',
        'client' : 'fanyideskweb',
        'salt' : salt,
        'sign': sign,
        'ts' : ts,
        'bv' : bv,
        'doctype': 'json',
        'version': '2.1',
        'keyfrom': 'fanyi.web',
        'action': 'FY_BY_CLICKBUTTION'
    }
    r = requests.post(url=url,data=data,headers=headers,cookies=cookies)
    content = r.json()
    print(content['translateResult'][0][0]['tgt'])

if __name__ == '__main__':
    url = 'http://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule'
    cookies = {'OUTFOX_SEARCH_USER_ID':'1653480669@101.169.1.84'}
    headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36','Referer': 'http://fanyi.youdao.com/'}
    translates()

Of course, this is just a foundation, and subsequent updates will also add Google translation and Baidu translation to form a desktop GUI...
GitHub address: https://github.com/uuzulien/study/tree/Tra... In the process of learning python, many people often give up because they can't solve problems, so I built a full stack of Python development and communication. Skirt: after a long time of fighting, I can find it under the conversion of streaming and thinking (homophony of numbers). There are the latest Python teaching projects to take, and the problems that I don't understand can be solved by the old driver, and they can supervise each other and make progress together

 

The text and pictures of this article come from the Internet and my own ideas. They are only for learning and communication. They have no commercial use. The copyright belongs to the original author. If you have any questions, please contact us in time for handling.

 

Tags: Python Windows JSON github

Posted on Wed, 12 Feb 2020 09:53:29 -0500 by ineedhelpbigtime