# -*- 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')
爱吃黄瓜的喵
- 粉丝: 90
- 资源: 47
最新资源
- 毕设和企业适用springboot全渠道电商平台类及供应链优化系统源码+论文+视频.zip
- 毕设和企业适用springboot人工智能类及全景数据分析平台源码+论文+视频.zip
- 毕设和企业适用springboot人工智能类及商业数据管理系统源码+论文+视频.zip
- 毕设和企业适用springboot人工智能类及情感分析平台源码+论文+视频.zip
- 毕设和企业适用springboot社交电商类及电商平台源码+论文+视频.zip
- 毕设和企业适用springboot社交电商类及供应链优化平台源码+论文+视频.zip
- 毕设和企业适用springboot社交电商类及环境监控平台源码+论文+视频.zip
- 毕设和企业适用springboot社交电商类及电子商务优化平台源码+论文+视频.zip
- 毕设和企业适用springboot全渠道电商平台类及跨境物流平台源码+论文+视频.zip
- 毕设和企业适用springboot全渠道电商平台类及企业IT解决方案平台源码+论文+视频.zip
- 毕设和企业适用springboot全渠道电商平台类及企业云管理平台源码+论文+视频.zip
- 毕设和企业适用springboot人工智能类及信息安全管理平台源码+论文+视频.zip
- 毕设和企业适用springboot人工智能类及视频会议系统源码+论文+视频.zip
- 毕设和企业适用springboot人工智能类及无人机管理平台源码+论文+视频.zip
- 毕设和企业适用springboot社交电商类及技术支持平台源码+论文+视频.zip
- 毕设和企业适用springboot社交电商类及旅游资源管理平台源码+论文+视频.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈