Jsoup的使用总结

官方文档链接

有疑惑先去看官方文档,官方的是最权威的,也是最全的。

 

 

 

当涉及到解析前端页面相关内容时,可以使用Jsoup进行解析,简单方便。

常用案例:爬虫爬取数据、对编辑器内容的操作,简而言之,只要发现div、html、img类似前端页面属性的内容需要做处理,用Jsoup就完了。

获取内容并重新赋值

本次案例是要将知识库中的某篇文章进行翻译,翻译的语种不确定,可以是英文的,可以使日文等等吧。如果直接全篇翻译,调用翻译接口后,因为使用的是编辑器编辑的,存在很多的样式属性,好多不该被翻译的都翻译了,导致最终无法使用。

于是有了以下代码,很简单的一个demo,因为只是给同事一点建议,所以主要是展示个意思,具体优化看自己发挥。

public static void main(String[] args) {
String test = "<div style=\"white-space: pre-wrap; line-height: 1.75; font-size: 14px;\">(1)首先安装小狐狸插件</div>\n" +
"<div style=\"white-space: pre-wrap; line-height: 1.75; font-size: 14px;\">首先进入谷歌浏览器&mdash;&gt;点击设置&mdash;&gt;点击扩展程序&mdash;&gt;搜索<span style=\"font-size: 16px;\">MetaMask&mdash;&gt;点击添加到chrome</span></div>\n" +
"<div style=\"white-space: pre-wrap; line-height: 1.75; font-size: 14px;\"><span style=\"font-size: 16px;\">(2)创建/导入钱包</span></div>\n" +
"<div style=\"white-space: pre-wrap; line-height: 1.75; font-size: 14px;\">安装成功后就会出现一下页面</div>\n" +
"<div style=\"white-space: pre-wrap; line-height: 1.75; font-size: 14px;\"><img src=\"https://cloud-files1.oss-cn-beijing.aliyuncs.com/jeditor/007_1643013939578.png\" alt=\"\" width=\"644\" height=\"399\" /></div>\n" +
"<div style=\"white-space: pre-wrap; line-height: 1.75; font-size: 14px;\">&nbsp;小狐狸的脸会跟着鼠标移动的哟。</div>\n" +
"<div style=\"white-space: pre-wrap; line-height: 1.75; font-size: 14px;\">然后就可以开始使用了,点击开始使用后,选择创建钱包</div>\n" +
"<div style=\"white-space: pre-wrap; line-height: 1.75; font-size: 14px;\">&nbsp;</div>\n" +
"<div style=\"white-space: pre-wrap; line-height: 1.75; font-size: 14px;\"><img src=\"https://cloud-files1.oss-cn-beijing.aliyuncs.com/jeditor/008_1643013962181.png\" alt=\"\" width=\"707\" height=\"376\" /></div>\n" +
"<div style=\"white-space: pre-wrap; line-height: 1.75; font-size: 14px;\"><img src=\"https://cloud-files1.oss-cn-beijing.aliyuncs.com/jeditor/009_1643013978477.png\" alt=\"\" width=\"739\" height=\"510\" /></div>\n" +
"<div style=\"white-space: pre-wrap; line-height: 1.75; font-size: 14px;\">&nbsp;</div>\n" +
"<div style=\"white-space: pre-wrap; line-height: 1.75; font-size: 14px;\">点击同意&nbsp;,观看官方注册帮助</div>\n" +
"<div style=\"white-space: pre-wrap; line-height: 1.75; font-size: 14px;\"><img src=\"https://cloud-files1.oss-cn-beijing.aliyuncs.com/jeditor/010_1643014008833.png\" alt=\"\" width=\"585\" height=\"528\" /></div>\n" +
"<div style=\"white-space: pre-wrap; line-height: 1.75; font-size: 14px;\"><img src=\"https://cloud-files1.oss-cn-beijing.aliyuncs.com/jeditor/011_1643014031920.png\" alt=\"\" width=\"378\" height=\"425\" /></div>\n" +
"<div style=\"white-space: pre-wrap; line-height: 1.75; font-size: 14px;\">&nbsp;&nbsp;这里稍微说明一下,下一步操作,会把你的助记词打乱,然后按照上一步操作得到的助记词顺序依次点击即可,如下</div>\n" +
"<div style=\"white-space: pre-wrap; line-height: 1.75; font-size: 14px;\"><img src=\"https://cloud-files1.oss-cn-beijing.aliyuncs.com/jeditor/012_1643014052087.png\" alt=\"\" width=\"524\" height=\"531\" /></div>\n" +
"<div style=\"white-space: pre-wrap; line-height: 1.75; font-size: 14px;\">&nbsp;</div>\n" +
"<div style=\"white-space: pre-wrap; line-height: 1.75; font-size: 14px;\">完成</div>\n" +
"<div style=\"white-space: pre-wrap; line-height: 1.75; font-size: 14px;\">&nbsp;</div>\n" +
"<div style=\"white-space: pre-wrap; line-height: 1.75; font-size: 14px;\"><img src=\"https://cloud-files1.oss-cn-beijing.aliyuncs.com/jeditor/013_1643014076342.png\" alt=\"\" width=\"551\" height=\"518\" /></div>\n" +
"<div style=\"white-space: pre-wrap; line-height: 1.75; font-size: 14px;\"><img src=\"https://cloud-files1.oss-cn-beijing.aliyuncs.com/jeditor/014_1643014103270.png\" alt=\"\" /></div>\n" +
"<div style=\"white-space: pre-wrap; line-height: 1.75; font-size: 14px;\">&nbsp;</div>\n" +
"<div style=\"white-space: pre-wrap; line-height: 1.75; font-size: 14px; text-align: left;\"><img src=\"https://cloud-files1.oss-cn-beijing.aliyuncs.com/jeditor/015_1643014124642.png\" alt=\"\" width=\"990\" height=\"429\" /></div>";
//使用Jsoup解析文本内容
Document document = Jsoup.parse(test);
//获取到所有div标签
Elements divs = document.getElementsByTag("div");
for (Element div : divs) {
//遍历所有div标签 并获取具体文本内容
String html = div.html();
//图片直接忽略 不进行翻译
if (!html.contains("img")){
//翻译接口 用的是百度提供的API 自己替换即可
String s = FanYiUtil.fanYi(html);
//将翻译后的结果重新赋值
Element html1 = div.html(s);
System.out.println(html1);
}else{
System.out.println(html);
}
}
}

待续......

 

posted @ 2022-01-25 11:18  背着泰山找黄河  阅读(165)  评论(0编辑  收藏  举报