Bal*_*usC 21
几种方式.您可以使用Element#append()将一些HTML附加到元素.
Document document = Jsoup.connect(url).get();
Element head = document.head();
head.append("<link rel=\"stylesheet\" href=\"http://example.com/your.css\">");
Run Code Online (Sandbox Code Playgroud)
或者,用于Element#attr(name, value)向现有元素添加属性.这是一个添加style="color:pink;"到所有链接的示例.
Document document = Jsoup.connect(url).get();
Elements links = document.select("a");
links.attr("style", "color:pink;");
Run Code Online (Sandbox Code Playgroud)
无论哪种方式,修改后获取最终的HTML字符串Document#html().
String html = document.html();
Run Code Online (Sandbox Code Playgroud)
通过PrintWriter#write()(使用正确的字符集)将其写入文件.
String charset = Jsoup.connect(url).response().charset();
// ...
Writer writer = new PrintWriter("/file.html", charset);
writer.write(html);
writer.close();
Run Code Online (Sandbox Code Playgroud)
最后在webview中打开它.因为我无法从头顶讲述它,所以这里只是一个我认为有用的示例的链接:WebViewDemo.java.顺便说一句,我在这个博客上找到了这个链接(我在Google 上发现了这个链接).