首页 编程教程正文

Python调用有道翻译接口

piaodoo 编程教程 2020-02-22 22:08:27 992 0 python教程

本文来源吾爱破解论坛

两个接口都发了,网上流传的translate和translate_o的都可以调用,对ip有次数限制,多次会被封,建议使用ip连接池
第一个调用translate接口,使用了ip连接池proxy_pool-master(github上有),需要Linux装SSDB,速度好慢嗨学网;当然也可以不用,去掉proxies参数即可,只不过会出现验证码,但是速度极佳。
第二个调用translate_o接口,两个接口似乎是独立的,验证码不相关。
业务代码主要是读取文件夹下的所有txt文件根据正则翻译指定内容,再输出到另一个文件夹下(只是为了汉化游戏而已,学Java的第一次写python,写的不好嗨学网,请大家多多指点),
大部分代码都是网上整理的,salt部分根据网站js进行了更新,应该最近还可用。

[Python] 纯文本查看 复制代码

# -*- coding: UTF-8 -*-import hashlib
import random
import requests
import time
import re
import os

##################################
def get_proxy():
    return requests.get("http://127.0.0.1:5010/get/").json()

def delete_proxy(proxy):
    requests.get("http://127.0.0.1:5010/delete/?proxy={}".format(proxy))

# your spider code

def getHtml(s, url, headers, data):
    # ....
    while True:
        retry_count = 2
        proxy = get_proxy().get("proxy")
        while retry_count > 0:
            try:
                # resp = s.post(self.url, headers=self.headers, data=data)
                html = s.post(url, headers=headers,data=data , proxies={"http": "http://{}".format(proxy)}, timeout=1)
                # 使用代{过}{滤}理访问
                return html.json()
            except Exception:
                
                retry_count -= 1
                print('代{过}{滤}理不通, 剩余机会:'+ str(retry_count))
        # 出错5次, 删除代{过}{滤}理池中代{过}{滤}理
        delete_proxy(proxy)
        print('删除代{过}{滤}理'+proxy)
    return None

##################################

s = requests.Session()
m = hashlib.md5()
class Dict:
    def __init__(self):
        self.headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36',
            'Referer': 'http://fanyi.youdao.com/',
            'contentType': 'application/x-www-form-urlencoded; charset=UTF-8',
            'Cookie': 'OUTFOX_SEARCH_USER_ID=-352392290@116.136.20.84; P_INFO=a121bc; OUTFOX_SEARCH_USER_ID_NCOO=710017829.1902944; JSESSIONID=aaaDa3sqezCDY-snjj91w; SESSION_FROM_COOKIE=unknown; ___rl__test__cookies='+str(int(time.time() * 1000))
        }
        self.url = 'http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule&sessionFrom='
        self.base_config()

    def base_config(self):
        """
        设置基本的参数,cookie
        """
        s.get('http://fanyi.youdao.com/')

    def translate(self,e):
        # i = "With what? I'll be out with my Mom at an expo for the next few days, but..."
        i = e
        self.headers['Content-Length']=str(233+len(e))
        ts = str(int(time.time() * 1000))
        salf = ts + str(random.randint(0, 9))
        n = 'fanyideskweb' + i + salf + "n%A-rKaT5fb[Gy?;N5@Tj"
        m.update(n.encode('utf-8'))
        sign = m.hexdigest()
        data = {
            'i': i,
            'from': 'AUTO',
            'to': 'AUTO',
            'smartresult': 'dict',
            'client': 'fanyideskweb',
            'salt': salf,
            'sign': sign,
            'ts': ts,
            'bv': '53539dde41bde18f4a71bb075fcf2e66',
            'doctype': 'json',
            'version': "2.1",
            'keyfrom': "fanyi.web",
            'action': "FY_BY_REALTlME"
        }

        ##########################################
        
        # resp = s.post(self.url, headers=self.headers, data=data)
        # rt = resp.json()
        rt = getHtml(s, self.url, self.headers, data)

        ####################
        
        arr = []
        for c in rt['translateResult'][0]:
            arr.append(c['tgt'])
        return ''.join(arr)

# dic = Dict()
# resp = dic.translate('hello girl')
# print(resp)


# 遍历文件夹
def copy(file, file2):
    for root, dirs, files in os.walk(file):
        # 遍历文件
        for f in files:
            # time.sleep(random.random())
            print(f)
            file_path = os.path.join(root, f)
            fi = open(file_path,'r',encoding='utf-8')
            fi2 =  open(os.path.join(file2, f),'w',encoding='utf-8')
            while True:
                line = fi.readline()
                if line.startswith("C_"):
                    dic = Dict()
                    line = re.sub(r'([\w\s]+)\|([^|]+)([.\s]+)',lambda m: m.group(1)+'|'+dic.translate(m.group(2))+m.group(3),line)
                    print('翻译成功:'+ line)
                fi2.write(line)
                if not line:
                    fi.close()
                    fi2.close()
                    break

copy("C:\\Users\\Administrator\\Desktop\\python-test\\dialogues","C:\\Users\\Administrator\\Desktop\\python-test\\def")


第二个

[Python] 纯文本查看 复制代码
import json
import random
import time
import hashlib
import requests
import re
import os
from urllib import parse
from urllib import request


def trans(content):
	# 等待用户输入需要翻译的单词
	i = content
	# 有道翻译的url链接
	url = 'http://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule'
	# 构造有道的加密参数
	client = "fanyideskweb"
	ts = int(time.time() * 1000)
	salt = str(ts + random.randint(1, 10))
	flowerStr = "n%A-rKaT5fb[Gy?;N5@Tj"
	sign = hashlib.md5((client + i + salt + flowerStr).encode('utf-8')).hexdigest()
	bv = '53539dde41bde18f4a71bb075fcf2e66'
	# 创建Form_Data字典,存储请求体
	Form_Data = {}
	# 需要翻译的文字
	Form_Data['i'] = i
	# 下面这些都先按照我们之前抓包获取到的数据
	Form_Data['from'] = 'AUTO'
	Form_Data['to'] = 'AUTO'
	Form_Data['smartresult'] = 'dict'
	Form_Data['client'] = client
	Form_Data['salt'] = salt
	Form_Data['sign'] = sign
	Form_Data['ts'] = ts
	Form_Data['bv'] = bv
	Form_Data['doctype'] = 'json'
	Form_Data['version'] = '2.1'
	Form_Data['keyfrom'] = 'fanyi.web'
	Form_Data['action'] = 'FY_BY_REALTIME'
	Form_Data['typoResult'] = 'false'
	# 对数据进行字节流编码处理
	data = parse.urlencode(Form_Data).encode('utf-8')
	# 创建Request对象
	req = request.Request(url=url, data=data, method='POST')
	# 写入header信息
	req.add_header('User-Agent', 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36')
	req.add_header('cookie', 'OUTFOX_SEARCH_USER_ID=-352392290@116.136.20.84')
	req.add_header('Referer', 'http://fanyi.youdao.com/')
	# 传入创建好的Request对象
	response = request.urlopen(req, timeout=5)
	# 读取信息并进行字节流解码
	html = response.read().decode('utf-8')
	print(html)
	# 把返回来的json字符串解析成字典
	translate_results = json.loads(html)
	# 打印翻译结果
	translate_result = translate_results["translateResult"][0][0]['tgt']
	print("翻译的结果是:%s" % translate_result)
	return translate_result

trans('Hey, Mrs. Juliana! Yeah, like a baby. What are you up to?')

def copy(file, file2):
    for root, dirs, files in os.walk(file):
        # 遍历文件
        for f in files:
            time.sleep(random.random())
            file_path = os.path.join(root, f)
            fi = open(file_path,'r',encoding='utf-8')
            fi2 =  open(os.path.join(file2, f),'w',encoding='utf-8')
            while True:
                line = fi.readline()
                if line.startswith("C_"):
                    line = re.sub(r'([\w\s]+)\|([^|]+)([.\s]+)',lambda m: m.group(1)+'|'+trans(m.group(2))+m.group(3),line)
                fi2.write(line)
                if not line:
                    fi.close()
                    fi2.close()
                    print(f)
                    break

# copy("C:\\Users\\Administrator\\Desktop\\python-test\\dialogues","C:\\Users\\Administrator\\Desktop\\python-test\\def")

版权声明:

本站所有资源均为站长或网友整理自互联网或站长购买自互联网,站长无法分辨资源版权出自何处,所以不承担任何版权以及其他问题带来的法律责任,如有侵权或者其他问题请联系站长删除!站长QQ754403226 谢谢。

有关影视版权:本站只供百度云网盘资源,版权均属于影片公司所有,请在下载后24小时删除,切勿用于商业用途。本站所有资源信息均从互联网搜索而来,本站不对显示的内容承担责任,如您认为本站页面信息侵犯了您的权益,请附上版权证明邮件告知【754403226@qq.com】,在收到邮件后72小时内删除。本文链接:https://www.piaodoo.com/7658.html

搜索