当前位置: 首页 > 知识库问答 >
问题:

Jsoup中的新行字符处理

鲁烨熠
2023-03-14

当使用JSoup解析html时,如果文本字符串中有新行字符,它会将其视为不存在。考虑:由于一个新行字符,此文本字符串将在此处换行。但是当JSoup解析这个字符串时,它返回,这个文本字符串将因为一个新行字符而被压缩。请注意,换行符甚至不会变成空格。我只想把它和一个空格一起退回。这是节点中的文本。我在stackoverflow上见过其他解决方案,人们希望或不希望在标记后换行。那不是我想要的。我只是想知道是否可以修改parse函数以返回不忽略新行字符。


共有2个答案

咸星波
2023-03-14

我想出来了。我从url获取html时出错。我使用的方法是:

public static String getUrl(String url) {
    URL urlObj = null;
    try{
        urlObj = new URL(url);
    }
    catch(MalformedURLException e) {
        System.out.println("The url was malformed!");
        return "";
    }
    URLConnection urlCon = null;
    BufferedReader in = null;
    String outputText = "";
    try{
        urlCon = urlObj.openConnection();
        in = new BufferedReader(new InputStreamReader(urlCon.getInputStream()));
        String line = "";
        while((line = in.readLine()) != null){
            outputText += line;
        }
        in.close();
    }
    catch(IOException e){
        System.out.println("There was an error connecting to the URL");
        return "no";
        }
    return outputText;
}

当我本应使用以下内容时:

public static String getUrl(String url) {
    URL urlObj = null;
    try{
        urlObj = new URL(url);
    }
    catch(MalformedURLException e) {
        System.out.println("The url was malformed!");
        return "";
    }
    URLConnection urlCon = null;
    BufferedReader in = null;
    String outputText = "";
    try{
        urlCon = urlObj.openConnection();
        in = new BufferedReader(new InputStreamReader(urlCon.getInputStream()));
        String line = "";
        while((line = in.readLine()) != null){
            outputText += line + "/n";
        }
        in.close();
    }
    catch(IOException e){
        System.out.println("There was an error connecting to the URL");
        return "no";
        }
    return outputText;
}

这个问题与JSoup无关。我想我会在这里注意到它,因为我从Ryan MitchellJava的即时网络抓取中复制了这段代码,任何遵循本教程的人都可能有同样的问题。

楚勇
2023-03-14

您能尝试一下吗,getWholeText基于这里的答案:防止Jsoup丢弃额外的空格

/**
 * @param cell element that contains whitespace formatting
 * @return
 */
public static String getText(Element cell) {
    String text = null;
    List<Node> childNodes = cell.childNodes();
    if (childNodes.size() > 0) {
        Node childNode = childNodes.get(0);
        if (childNode instanceof TextNode) {
            text = ((TextNode)childNode).getWholeText();
        }
    }
    if (text == null) {
        text = cell.text();
    }
    return text;
}
 类似资料:
  • 我有下面的Java内容,我只想剥离html标记,而不是新行字符 如果我在文本丰富编辑器中打开上面的内容,第1行和第2行以不同的行显示(不显示

  • 主要内容:Jsoup 解析字符串 语法,Jsoup 解析字符串 说明,Jsoup 解析字符串 示例以下示例将展示将 HTML 字符串解析为 Document 对象。 Jsoup 解析字符串 语法 document : 文档对象代表 HTML DOM。 Jsoup : 解析给定 HTML 字符串的主类。 html : HTML 字符串。 Jsoup 解析字符串 说明 parse(String html) 方法将输入的 HTML 解析为一个新的 Document。该文档对象可用于遍历和获取 htm

  • 字符串池是否驻留在堆上?如果是,字符串文字是否符合垃圾回收机制? 当使用时,我们知道它会在堆上创建一个对象,并将String文字放在String池中。所以我的第二个问题是:

  • 我是使用Jsoup的新手,我遇到了一个问题,无法从类名text为字符串的div中获取文本值。这是一个想要丢弃的字符串。 谁能帮帮我。非常感谢。

  • 假设如下: 哦哦! 有人能解释一下方法在处理字符串时应该有什么行为吗?

  • 问题内容: 我在使用jsoup时遇到问题,我想从URL中获取一个文档,该文档将根据不起作用的元刷新URL重定向到另一个URL,以明确说明我是否输入了名为http://的网站URL www.amerisourcebergendrug.com,它将根据元刷新URL 自动重定向到http://www.amerisourcebergendrug.com/abcdrug/,但我的jsoup仍然坚持使用htt