有道翻译接口(无反爬)
有道翻译接口(无反爬)

有道翻译接口(无反爬)

# -*- coding: UTF-8 -*-
"""=========================================================
@Project -> File: commodity_trading_spider -> youdao
@IDE: PyCharm
@author: lxc
@date: 2023/4/11 上午 10:26
@Desc:
功能描述:
有道词典接口-中英翻译
"""
import json
from utils.get_content import get_html, post_html
from utils.proxy import get_proxy

def get_dict_translation(query):
    """
    词典查询
    :param query:查询词
    :return:查词结果
    """
    headers = {
        "Host": "dict.youdao.com",
        "Pragma": "no-cache",
        "Cache-Control": "no-cache",
        "sec-ch-ua": "\"Chromium\";v=\"110\", \"Not A(Brand\";v=\"24\", \"Microsoft Edge\";v=\"110\"",
        "Accept": "application/json, text/plain, */*",
        "sec-ch-ua-mobile": "?0",
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36 Edg/110.0.1587.63",
        "sec-ch-ua-platform": "\"Windows\"",
        "Origin": "https://www.youdao.com",
        "Sec-Fetch-Site": "same-site",
        "Sec-Fetch-Mode": "cors",
        "Sec-Fetch-Dest": "empty",
        "Referer": "https://www.youdao.com/",
        "Accept-Language": "zh-CN,zh;q=0.9"
    }
    url = "https://dict.youdao.com/suggest"
    params = {
        "num": "5",
        "ver": "3.0",
        "doctype": "json",
        "cache": "false",
        "le": 'en',
        # "le": 'ko',
        "q": query
    }
    response = get_html(url, headers=headers, params=params, proxies=get_proxy())
    res = []
    if response.status_code == 200:
        result = response.json()
        if result['result'].get("code") == 200:
            res = result['data'].get("entries")
    return res

def get_text_translation(query):
    """
    文本翻译
    :param query:查询词
    :return: 直译结果
    """
    headers = {
        "Host": "dict.youdao.com",
        "Pragma": "no-cache",
        "Cache-Control": "no-cache",
        "sec-ch-ua": "\"Chromium\";v=\"110\", \"Not A(Brand\";v=\"24\", \"Microsoft Edge\";v=\"110\"",
        "Accept": "application/json, text/plain, */*",
        "sec-ch-ua-mobile": "?0",
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36 Edg/110.0.1587.63",
        "sec-ch-ua-platform": "\"Windows\"",
        "Origin": "https://www.youdao.com",
        "Sec-Fetch-Site": "same-site",
        "Sec-Fetch-Mode": "cors",
        "Sec-Fetch-Dest": "empty",
        "Referer": "https://www.youdao.com/",
        "Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6"
    }
    url = "https://dict.youdao.com/jsonapi_s"
    params = {
        "doctype": "json",
        "jsonversion": "4"
    }
    data = {
        "q": query,
        "le": "en",
        "t": "0",
        "client": "web",
        "keyfrom": "webdict"
    }
    tran = ""
    for _ in range(5):
        response = post_html(url, headers=headers, params=params, data=data, proxies=get_proxy())
        if response.status_code == 200:
            res = response.json()
            tran = res.get("fanyi", {}).get("tran")
            if tran:
                return tran
            else:
                continue
    return tran
if __name__ == '__main__':
    # text = "最近拿锐龙97900X玩超频的时候,直接超过头导致开机不能,再次见识了这一代AMD主板在某些方面的“智障”(包括打人硕也一样),不过拆机抠电池的时候,顺带将RTX4080换下来,打算安装上前不久买的RX7900XT。这当然不是我们不知道RTX4080要比RX7900XT好用,纯粹是为了好玩罢了,或者就是想告诉大家:俺们的高端显卡真的不少!"
    # res = get_translation('新字段')
    text = "一个新增字段"
    res = get_text_translation(text)
    print(res)

发表回复

您的电子邮箱地址不会被公开。