我正在使用Jsoup从URL阅读文本。以下链接提供了一些在将正文转换为文本时保留换行符的技巧,如何在使用jsoup将html转换为纯文本时保留换行符?
我使用以下几行代码来转换标签
String prettyPrintedBodyFragment = Jsoup.clean(body, "", Whitelist
.none().addTags("br", "p", "h1"), new OutputSettings()
.prettyPrint(true));
System.out.println(prettyPrintedBodyFragment);
我仍然在单行中获得正文/内容。有什么线索吗?
编辑:这是完整的源代码,我只看到一行输出
public static void main(String[] args) throws Exception {
Connection conn = Jsoup.connect("http://finance.yahoo.com/");
Document doc = conn.get();
String body = doc.body().text();
String prettyPrintedBodyFragment = Jsoup.clean(body, "", Whitelist
.none().addTags("br", "p", "h1"), new OutputSettings()
.prettyPrint(true));
System.out.println(prettyPrintedBodyFragment);
}
更改:
String body = doc.body().text();
至:
String body = doc.body().html();
由于您已经在转储标签,Whitelist
因此在格式化文本时无法将其包括在内。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句