JSOUP 抽取,插入内容
static String getHtml(Article article) { String csspath = article.getTemplate().getSavePath(); String content = article.getArticleContent().getContent(); StringBuffer headStr = new StringBuffer(); String style = article.getArticleContent().getStyle(); String eg_pic = ""; String pic_size = "@1000w_1o.jpg"; Document doc = Jsoup.parse(content.replaceAll(" replaced=\"true\"", "")); Elements body = doc.body().children(); //内容图片此尺寸控制 String pic_class = "figure > img"; Elements es_pic = doc.select(pic_class); int pic_index = 0; for (Element picdesc : es_pic) { String rp_src = picdesc.attr("src") + pic_size; body.select("figure > img").get(pic_index).attr("src", rp_src); eg_pic = pic_index == 0 ? rp_src : eg_pic; pic_index++; } //头图片尺寸 String pic_header_class = "header > img"; Elements es_header_pic = doc.select(pic_header_class); for (Element pic_header : es_header_pic) { String old_pic_header = pic_header.attr("src"); String rp_h_src = old_pic_header + pic_size; body.select("header > img").get(0).attr("src", rp_h_src); } Document body_new = Jsoup.parse(body.toString()); //OGP String img = article.getCoverUrl(); if (null == img || img.isEmpty()) { if (eg_pic.isEmpty()) { img = "https://www.duweixin.net/app/img/logo.jpg"; } else { img = eg_pic.replace("@1000w_1o.jpg", "@400w_1o.jpg"); } } else { img += "@400w_1o.jpg"; } StringBuffer styleBuffer = new StringBuffer(); //字体样式 if (null != style && !style.isEmpty()) { styleBuffer.append("\n"); String[] styles = style.split(","); for (int i = 0; i "); } headStr.append(""); headStr.append(""); headStr.append(""); headStr.append(""); headStr.append(""); headStr.append(""); headStr.append(""); headStr.append("\n"); headStr.append("\n"); headStr.append("\n"); headStr.append(""); headStr.append(styleBuffer.toString()); Element head_add = doc.select("head").first(); head_add.append(headStr.toString()); Element body_add = body_new.select("body").first(); body_add.append(""); content = "\n" + "\n" + head_add.toString() + "\n" + body_add.toString() + "\n"; return content; }
关键字:jsoup
版权声明
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处。如若内容有涉嫌抄袭侵权/违法违规/事实不符,请点击 举报 进行投诉反馈!