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);
|
}
|
|
|
}
|