python翻譯 Python翻譯

2021-10-10 02:19:37 字數 3341 閱讀 1607

translator.py

# -*- coding: utf-8 -*-

# author: inspurer(月小水長)

# pc_type lenovo

# create_time: 2019/4/6 15:44

# file_name: translator.py

# github

# qq郵箱 [email protected]

import requests

from handlejs import py4js

import win32clipboard as w

import win32con

import time

def gettext():#讀取剪下板

# 開啟剪貼簿

w.openclipboard()

# 讀取剪貼簿的內容

d = w.getclipboarddata(win32con.cf_text)

# 關閉剪貼簿

w.closeclipboard()

try:

return d.decode('utf-8')

except:

return d.decode('gbk')

def settext(astring):#寫入剪下板

w.openclipboard()

w.emptyclipboard()

w.setclipboardtext(astring)

w.closeclipboard()

def translate(tk, content):

if len(content) > 4891:

print("翻譯的長度超過限制!!!")

return

param =

result = requests.get("""

&tl=zh-cn&hl=zh-cn&dt=at&dt=bd&dt=ex&dt=ld&dt=md&dt=qca&dt=rw&dt=rm&dt=ss

&dt=t&ie=utf-8&oe=utf-8&clearbtn=1&otf=1&pc=1&srcrom=0&ssel=0&tsel=0&kc=2""", params=param)

data = result.json()

# print(data)

print(data[0][0][0])

print(data[0][1][2])

return data[0][0][0]

def main():

js = py4js()

ls = ''

while(true):

# 如果剪貼簿正在被占用

try:

cs = gettext()

except:

time.sleep(1)

cs = gettext()

print('cs',cs)

if cs and cs != ls:

print('準備翻譯')

content = gettext()

tk = js.gettk(content)

res = translate(tk, content)

settext(res)

time.sleep(1)

ls = res

if __name__ == "__main__":

main()

# i am a girl

# i am a boy

handlejs.py

# -*- coding: utf-8 -*-

# author: inspurer(月小水長)

# pc_type lenovo

# create_time: 2019/4/6 15:49

# file_name: handlejs.py

# github

# qq郵箱 [email protected]

import execjs

class py4js():

def __init__(self):

self.ctx = execjs.compile("""

function tl(a) {

var k = "";

var b = 406644;

var b1 = 3293161072;

var jd = ".";

var $b = "+-a^+6";

var zb = "+-3^+b+-f";

for (var e = , f = 0, g = 0; g < a.length; g++) {

var m = a.charcodeat(g);

128 > m ? e[f++] = m : (2048 > m ? e[f++] = m >> 6 | 192 : (55296 == (m & 64512) && g + 1 < a.length && 56320 == (a.charcodeat(g + 1) & 64512) ? (m = 65536 + ((m & 1023) << 10) + (a.charcodeat(++g) & 1023),

e[f++] = m >> 18 | 240,

e[f++] = m >> 12 & 63 | 128) : e[f++] = m >> 12 | 224,

e[f++] = m >> 6 & 63 | 128),

e[f++] = m & 63 | 128)

a = b;

for (f = 0; f < e.length; f++) a += e[f],

a = rl(a, $b);

a = rl(a, zb);

a ^= b1 || 0;

0 > a && (a = (a & 2147483647) + 2147483648);

a %= 1e6;

return a.tostring() + jd + (a ^ b)

function rl(a, b) {

var t = "a";

var yb = "+";

for (var c = 0; c < b.length - 2; c += 3) {

var d = b.charat(c + 2),

d = d >= t ? d.charcodeat(0) - 87 : number(d),

d = b.charat(c + 1) == yb ? a >>> d: a << d;

a = b.charat(c) == yb ? a + d & 4294967295 : a ^ d

return a

def gettk(self, text):

return self.ctx.call("tl", text)

python有道翻譯 Python版有道翻譯

做過爬蟲的朋友應該很通俗易懂,話不多說 亮 吧。需要用到的模組 urllib time hashlib random json code encoding utf 8 author search plugname youdaodict import urllib.request import url...

python單詞翻譯 python 中英文翻譯

例項簡介 例項截圖 核心 import urllib.request 匯入urllib模組,主要功能是獲取網頁資料,request用於模擬傳送請求 import urllib.parse parse提供url處理 import json 匯入json模組,是一種輕量級的資料交換格式,現在也常用於ht...

python英文翻譯 python中英文翻譯

實現 現在該 post 的 data 都有了,開始正式寫 啦,下面是程式的 每行我都有詳細的注釋,相信大家通過上面的分析也是可以看明白的。import time 時間戳 import json 返回json 處理 import requests 請求 url import hashlib md5 加...