gpt4 book ai didi

java - 使用 jsoup 解析 html 时避免删除空格和换行符

转载 作者:搜寻专家 更新时间:2023-11-01 02:59:23 24 4
gpt4 key购买 nike

我有一个示例代码如下。

String sample = "<html>
<head>
</head>
<body>
This is a sample on parsing html body using jsoup
This is a sample on parsing html body using jsoup
</body>
</html>";

Document doc = Jsoup.parse(sample);
String output = doc.body().text();

我得到的输出为

This is a sample on parsing html body using jsoup This is a sample on `parsing html body using jsoup`

但我希望输出为

This is a sample on              parsing html body using jsoup
This is a sample on parsing html body using jsoup

如何解析它以便获得此输出?或者在 Java 中有另一种方法吗?

最佳答案

您可以禁用文档的 pretty-print 以获得您想要的输出。但是您还必须将 .text() 更改为 .html()

Document doc = Jsoup.parse(sample);
doc.outputSettings(new Document.OutputSettings().prettyPrint(false));
String output = doc.body().html();

关于java - 使用 jsoup 解析 html 时避免删除空格和换行符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40396524/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com