Python3呼叫Google翻譯

2021-10-03 05:18:52 字數 1770 閱讀 7240

import urllib.request    

from handlejs import py4js

def open_url(url):

headers =

req = urllib.request.request(url = url,headers=headers)

response = urllib.request.urlopen(req)

data = response.read().decode('utf-8')

return data

def translate(content,tk):

if len(content) > 4891:

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

return

content = urllib.parse.quote(content)

url = ""+ "&sl=en&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&tk=%s&q=%s"%(tk,content)

#返回值是乙個多層巢狀列表的字串形式,解析起來還相當費勁,寫了幾個正則,發現也很不理想,

#後來感覺,使用正則簡直就是把簡單的事情複雜化,這裡直接切片就ok了

result = open_url(url)

end = result.find("\",")

if end > 4:

print(result[4:end])

def main():

js = py4js()

while 1:

content = input("輸入待翻譯內容:")

if content == 'q!':

break

tk = js.gettk(content)

translate(content,tk)

if __name__ == "__main__":

main()

import execjs  

class py4js():

def __init__(self):

self.ctx = execjs.compile("""

function tl(a)

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)

return a

} """)

def gettk(self,text):

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

使用python3呼叫谷歌翻譯成中文

日常Python 3 呼叫函式

任務.輸出每日一貼 在idle中建立乙個名為function tips.py的檔案,然後在該檔案中建立乙個名稱為function tips的函式,在該函式中,從勵志文字列表中獲取一條勵志文字並輸出,最後再呼叫函式function tips 如下 coding gbk def function tip...

python3呼叫http介面

最近在寫python呼叫介面的服務,寫了乙個呼叫介面的函式,如下 param inputdata 單個樣本的輸入引數,是json格式的資料 return 單個樣本的探真查詢變數結果資料 呼叫介面 介面有正確的資料才讀入,否則為空 if res code 0000 res data json.load...

Python3呼叫webservice介面

工作中偶爾需要對接webservice的介面 網上搜了一下,不少的教程推薦使用suds。但是suds這個包早就停止維護了。對於簡單的呼叫,我還是覺得自己寫更方便點,夠用就行。介面不同,訊息體和需要傳入的引數也是不同的。body可以參照你需要請求的webservice介面的資料樣例 body str ...