admin
2025-02-11 8cc47cfe4c6d6b48e62cf00f6cbd0951ec57c264
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package com.everyday.word.utils;
 
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
 
import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
 
/**
 * @author hxh
 * @title: YouDaoWebApi
 * @description: 有道网页API
 * @date 2024/9/14 15:48
 */
public class YouDaoWebApi {
 
    private static ScriptEngineManager scriptEngineManager = new ScriptEngineManager();
 
    private static String withDrawValidData(String script) {
        StringBuffer sb = new StringBuffer("var _window={};\n");
        sb.append(script.replace("window.", "_window."));
        sb.append("\nfunction getData(){return  JSON.stringify(_window.__NUXT__);};");
        // 获取 JavaScript 引擎
        ScriptEngine engine = scriptEngineManager.getEngineByName("javascript");
        // 定义要执行的 JavaScript 代码
        try {
            // 执行 JavaScript 代码
            engine.eval(sb.toString());
            Invocable invocableEngine = (Invocable) engine;
            Object callbackvalue = invocableEngine.invokeFunction("getData");
            if (callbackvalue != null) {
                return callbackvalue.toString();
            }
            // 输出结果
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
 
    /**
     * @return java.lang.String
     * @author hxh
     * @description 从页面中提取有效数据
     * @date 16:19 2024/9/14
     * @param: url
     **/
    private static String getPageData(String url) throws IOException {
        Document doc = Jsoup.connect(url).userAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/536.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/536.36").timeout(30 * 1000).get();
        Elements els = doc.getElementsByTag("script");
        for (int i = 0; i < els.size(); i++) {
            Element e = els.get(i);
            String text = e.html();
            if (text.contains("window.__NUXT__=")) {
                return withDrawValidData(text);
            }
        }
        return null;
    }
 
    /**
     * @return java.lang.String
     * @author hxh
     * @description 获取单词的信息
     * @date 16:24 2024/9/14
     * @param: spelling
     **/
    public static String getInfoBySpelling(String spelling) throws IOException {
        String value = getPageData(String.format("https://www.youdao.com/result?word=%s&lang=en", URLEncoder.encode(spelling, "UTF-8")));
        return value;
    }
 
    /**
     * @return java.lang.String
     * @author hxh
     * @description 获取单词的例句
     * @date 16:24 2024/9/14
     * @param: spelling
     **/
    public static String getLJBySpelling(String spelling) throws IOException {
        String value = getPageData(String.format("https://www.youdao.com/result?word=%s&lang=en", URLEncoder.encode("lj:" + spelling, "UTF-8")));
        return value;
    }
 
 
    public static void main(String[] args) throws IOException {
        String value = getInfoBySpelling("administrator");
        System.out.println(value);
        value = getLJBySpelling("administrator");
        System.out.println(value);
    }
 
 
}