# -*- coding: utf-8 -*-
import execjs
import requests
import json
import urllib3.contrib.pyopenssl
#----------------------------------
#这里的tkk值需要进行后期喜欢,需要根据tkk.js这个文件进行计算操作,具体的计算请使用node js去进行,命令行就能搞定
def get_tk(text,tkk='426538.4063001528'):
jsstr = get_js()
ctx = execjs.compile(jsstr) #加载JS文件
return (ctx.call('tk',text,tkk)) #调用js方法 第一个参数是JS的方法名,后面的data和key是js方法的参数
def get_js():
f = open("tk.js", 'r', encoding='utf-8') # 打开JS文件
line = f.readline()
htmlstr = ''
while line:
htmlstr = htmlstr+line
line = f.readline()
return htmlstr
def write_txt(file_name,info):
fp = open(file_name, "a",encoding='utf-8') # 打开一个文本文件
fp.write(info+'\n') # 写入数据
fp.close() # 关闭文件
def google_tran(from_lang, to_lang, tran_text):
tk = get_tk(tran_text)
url = "https://translate.google.cn/translate_a/single?client=t&sl=" + from_lang + "&tl=" + to_lang + "&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&source=btn&ssel=3&tsel=3&kc=0&tk=" + tk + "&q=" + tran_text
file_name = "tran.txt"
txt_file_path = file_name
# requests.packages.urllib3.disable_warnings()
# r = requests.get(url ,verify=False ,headers={'Connection':'close'})
# requests.adapters.DEFAULT_RETRIES = 5
# s = requests.session()
# s.keep_alive = False
#免费代理使用网站
#http://ip.zdaye.com/shanghai_ip.html#Free
s = requests.session()
# url = "https://mail.163.com/"
s.keep_alive = False
s.proxies = {
"http": "210.72.14.142:80",
"http": "106.14.5.129:8080",
"http": "222.73.68.144:8090" ,
"http": "211.95.2.37:3128" ,
"http": "59.46.0.31:1080" ,
"http": "120.198.248.30:8080" ,
"http": "112.74.39.165:8080" ,
"http": "211.138.61.27:80" ,
}
s.headers = {'Connection':'close'}
r = s.get(url)
with open(txt_file_path, "wb") as code:
code.write(r.content)
try:
byte_to_string = str(r.content, encoding="utf-8")
string_to_list = json.loads(byte_to_string)
return string_to_list
except Exception as e:
print(e)
return ""
'''
调用范例:
from_lang = "zh-CN"
to_lang = "en"
tran_text = "大家好"
result = google_tran(from_lang, to_lang, tran_text)
print(len(result))
print(result[0][0][0])
print('ok')
en 英语
tr 土语
uz 乌语
'''
if __name__ == '__main__':
from_lang = "zh-CN"
to_lang = "uz"
tran_text = "你好"
result = google_tran(from_lang, to_lang, tran_text)
print(len(result))
print(result[0][0][0])
# num_sql_string = "SELECT COUNT(id) AS num FROM is_sb_ug_cn WHERE is_fyj_tran=0 AND dr=0"
# num_results = db_select(num_sql_string, "127.0.0.1", 3306, "root", "123456", "ml_data")
# num = num_results[0]
# count_rows = num[0]
# print(count_rows)
print('ok')
爱吃黄瓜的喵
- 粉丝: 89
- 资源: 47
最新资源
- 日志文件:日志概念、LogBack日志技术的概述、使用、logback.xml配置文件详解
- 基于python使用Drl来解决多智能体卸载问题+源码(期末作业&课程设计&项目开发)
- 科学计算领域中的Fortran语言基础知识与应用
- 4.健身房预约课程-微信小程序.zip
- 小乌龟键盘控制源码111111
- 电赛2023年本科组电子电路设计比赛指南与任务解析
- Delphi 12 控件之dspack For Delphi 10.2 - 视频播放组件包e963a-main.zip
- delphi 12 控件之FB4D – The OpenSource Cross-Platform Library for FirebaseFB4D-master.zip
- Rust语言入门与进阶教程
- delphi 12 控件之Delphi开发的微信电脑版登录工具ec617-main.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈