我正在使用 Jsoup 从 URL 读取文本。以下链接有一些提示,可在将正文转换为文本时保留新行 How do I preserve line breaks when using jsoup to convert html to plain text?
我使用以下行来转换标签
String prettyPrintedBodyFragment = Jsoup.clean(body, "", Whitelist
.none().addTags("br", "p", "h1"), new OutputSettings()
.prettyPrint(true));
System.out.println(prettyPrintedBodyFragment);
我仍然在单行中获取正文/内容。有什么线索吗?
编辑:这是完整的源代码,我只看到 1 行的输出
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();
由于您已经转储标签,因此您的白名单
无法在格式化文本时包含它们。
关于java - 无法保留从 URL 读取的文本中的换行符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21667720/