//前半部分可以引入一个url,去解析url
String url = "https://www.yidianzixun.com/article/0PU75ct0";
try{
Document parse = Jsoup.connect(url).timeout(5000).get();//这里是解析url
}catch (Exception e){
e.printStackTrace();
}
//后半部分往下看
//****************************************************************
//这个方法框架可以不用,看其中的代码块就可以
@Override
public boolean pro(Map<String, Object> map) throws Exception {
String content = (String) map.get("clean_content");//clean_content字段就是HTML文本内容
Document doc = Jsoup.parseBodyFragment(content);//这里是直接去解析文本
String[] tags = new String[]{"div","section", "p","ul","li","ol","br","h1","h2","h3","strong"};
for(String tag : tags) {
Elements select = doc.select("body").select(tag);
for (Element element : select) {
String text = element.text();
Elements children = element.children();
//判断标签中的文本是否为空和标签中是否还含有子标签
if("".equals(text) && (children == null || children.isEmpty())){
element.getAllElements().remove();
}
}
}
return true;
}
Jsoup去除HTML空标签
最新推荐文章于 2023-04-16 16:32:14 发布