Jsoup地址以及相关文档:https://jsoup.org/
NewsyunUtils
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
|
public static String handleContentBodyHtml(String contentText) { Document document = Jsoup.parse(contentText); Element body = document.body();
Elements imgs = body.getElementsByTag("img"); for (Element img : imgs) { if (!img.attr("src").startsWith("http") && !img.attr("src").startsWith("https")) { img.attr("src", HttpConstants.HOST_IP + img.attr("src")); } }
Elements objects = body.getElementsByTag("object"); for (Element object : objects) { String nodeName = object.parentNode().nodeName(); if (nodeName.equals("body")) {
} }
Elements audios = body.getElementsByTag("audio"); for (Element audio : audios) { if (!audio.attr("src").startsWith("http") && !audio.attr("src").startsWith("https")) { audio.attr("src", HttpConstants.HOST_IP + audio.attr("src")); } audio.removeAttr("style"); audio.removeAttr("loop"); audio.removeAttr("autoplay"); audio.addClass("newsyun_video_or_audio"); audio.attr("id", "newsyun_video_or_audio"); audio.children().remove(); audio.attr("width", "100%"); audio.attr("height", "50px"); }
Elements videos = body.getElementsByTag("video"); for (Element video : videos) { if (!video.attr("src").startsWith("http") && !video.attr("src").startsWith("https")) { video.attr("src", HttpConstants.HOST_IP + video.attr("src")); } video.removeAttr("style"); video.removeAttr("loop"); video.removeAttr("autoplay"); video.addClass("newsyun_video_or_audio"); video.attr("id", "newsyun_video_or_audio"); video.children().remove(); video.attr("width", "100%"); video.attr("height", "200px");
} return body.toString(); }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
public static List<String> handleContentBodyImgHtml(String contentText) {
List<String> mListForContentImg = new ArrayList<>();
Document document = Jsoup.parse(contentText); Element body = document.body(); Elements imgs = body.getElementsByTag("img"); for (Element img : imgs) { if (!img.attr("src").startsWith("http") && !img.attr("src").startsWith("https")) { img.attr("src", HttpConstants.HOST_IP + img.attr("src")); } mListForContentImg.add(img.attr("src")); } return mListForContentImg; }
|