Python 调用腾讯翻译君 (2021/1/13 ) 亲测可用

import requests
import time

class Tencent():
    def __init__(self):
        self.api_url = 'https://fanyi.qq.com/api/translate'
        self.headers = {
            'Cookie': 'fy_guid=605ead81-f210-47eb-bd80-ac6ae5e7a2d8; '
                      'qtv=ed286a053ae88763; '
                      'qtk=wfMmjh3k/7Sr2xVNg/LtITgPRlnvGWBzP9a4FN0dn9PE7L5jDYiYJnW03MJLRUGHEFNCRhTfrp/V+wUj0dun1KkKNUUmS86A/wGVf6ydzhwboelTOs0hfHuF0ndtSoX+N3486tUMlm62VU4i856mqw==; ',
            'Host': 'fanyi.qq.com',
            'Origin': 'https://fanyi.qq.com',
            'Referer': 'https://fanyi.qq.com/',
            'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, '
                          'like Gecko) Chrome/73.0.3683.86 Safari/537.36', }
        self.fromlang = 'auto'
        self.text = ''
        self.tolang = 'en'  # 设置默认为英语
        self.sessionUuid = str(int(time.time() * 1000))
        self.fy_guid, self.qtv, self.qtk = self.get_qtv_qtk()
        self.headers['Cookie'] = self.headers['Cookie'].replace(
            '605ead81-f210-47eb-bd80-ac6ae5e7a2d8', self.fy_guid)
        self.headers['Cookie'] = self.headers['Cookie'].replace(
            'ed286a053ae88763', self.qtv)
        self.headers['Cookie'] = self.headers['Cookie'].replace(
            'wfMmjh3k/7Sr2xVNg/LtITgPRlnvGWBzP9a4FN0dn9PE7L5jDYiYJnW03MJLRUGHEFNCRhTfrp/V+wUj0dun1KkKNUUmS86A/wGVf6ydzhwboelTOs0hfHuF0ndtSoX+N3486tUMlm62VU4i856mqw==',
            self.qtk)

    def get_filter(self, text):
        if isinstance(text, list):
            text = ''.join(text)
        text = str(text)
        text = text.strip()
        filter_list = [
            '\r', '\n', '\t', '\u3000', '\xa0', '\u2002',
            '<br>', '<br/>', '    ', '    ', ' ', '>>', '"',
            '展开全部', ' '
        ]
        for fl in filter_list:
            text = text.replace(fl, '')
        return text

    def get_qtv_qtk(self):
        api_url = 'https://fanyi.qq.com/api/aaa123'
        headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, '
                          'like Gecko) Chrome/73.0.3683.86 Safari/537.36', }
        res = requests.post(api_url, headers=headers)
        data = res.json()
        fy_guid = res.cookies.get('fy_guid')
        # reg = re.compile(r'var qtv = "(.*?)"')
        qtv = data['qtv']
        # reg = re.compile(r'var qtk = "(.*?)"')
        qtk = data['qtk']
        return fy_guid, qtv, qtk

    def getHtml(self, url, headers, data):
        try:
            html = requests.post(url=url, data=data, headers=headers)
            # print(html.text)
            datas = html.json()['translate']['records']
            if html != None and datas != None:
                # 以文本的形式打印结果
                trans_result = ''.join([data['targetText'] for data in datas])
            return trans_result
        except Exception:
            return None

    def translate(self, text):
        data = {
            'source': self.fromlang,
            'target': self.tolang,
            'sourceText': text,
            'qtv': self.qtv,
            'qtk': self.qtk,
            'sessionUuid': self.sessionUuid, }
        try:
            result = self.getHtml(self.api_url, self.headers, data)
            # print('腾讯翻译结果为:',result)
            return result
        except:
            return '程序出现了一点小问题,无法翻译'

t= Tencent().translate('yes')
print(t)
# Tencent().get_qtv_qtk()

声明:该文章系转载,转载该文章的目的在于更广泛的传递信息,并不代表本网站赞同其观点,文章内容仅供参考。

本站是一个个人学习和交流平台,网站上部分文章为网站管理员和网友从相关媒体转载而来,并不用于任何商业目的,内容为作者个人观点, 并不代表本网站赞同其观点和对其真实性负责。

我们已经尽可能的对作者和来源进行了通告,但是可能由于能力有限或疏忽,导致作者和来源有误,亦可能您并不期望您的作品在我们的网站上发布。我们为这些问题向您致歉,如果您在我站上发现此类问题,请及时联系我们,我们将根据您的要求,立即更正或者删除有关内容。本站拥有对此声明的最终解释权。