使用 Jsoup 保留行

发布于 2024-12-18 02:07:11 字数 1367 浏览 0 评论 0原文

我正在使用 Jsoup 从 html 获取一些数据,我有这样的代码:

System.out.println("nie jest");
StringBuffer url=new StringBuffer("http://www.darklyrics.com/lyrics/");
url.append(args[0]);
url.append("/");
url.append(args[1]);
url.append(".html");

//wyciaganie odpowiednich klas z naszego htmla
Document doc=Jsoup.connect(url.toString()).get();
Element lyrics=doc.getElementsByClass("lyrics").first();
Element tracks=doc.getElementsByClass("albumlyrics").first();

//Jso
//lista sciezek
int numberOfTracks=tracks.getElementsByTag("a").size();

一切都会好的,我提取我想要的数据,但是当我这样做时:

lyrics.text()

我得到的文本没有换行符,所以我想知道如何留下换行符在显示的文本中,我在 stackoverflow 上阅读了有关此事的其他线程,但它们没有帮助,我尝试做这样的事情:

TextNode tex=TextNode.createFromEncoded(lyrics.text(), lyrics.baseUri());

但我无法通过换行符获得我想要的文本。我查看了之前关于此问题的帖子, 使用 JSoup 删除 HTML 实体,同时保留换行符 但我达不到我想要的效果。我应该怎么办?

编辑:我得到了我想要的效果,但我认为这不是很好的解决方案:

for (Node nn:listOfNodes)
            {
                String s=Jsoup.parse(nn.toString()).text();
                if ((nn.nodeName()=="#text" || nn.nodeName()=="h3"))
                {
                    buf.append(s+"\n");

                }
            }

有人有更好的主意吗?

I am using Jsoup to get some data from html, I have this code:

System.out.println("nie jest");
StringBuffer url=new StringBuffer("http://www.darklyrics.com/lyrics/");
url.append(args[0]);
url.append("/");
url.append(args[1]);
url.append(".html");

//wyciaganie odpowiednich klas z naszego htmla
Document doc=Jsoup.connect(url.toString()).get();
Element lyrics=doc.getElementsByClass("lyrics").first();
Element tracks=doc.getElementsByClass("albumlyrics").first();

//Jso
//lista sciezek
int numberOfTracks=tracks.getElementsByTag("a").size();

Everything would be fine, I extracthe data I want, but when I do:

lyrics.text()

I get the text with no line breaks, so I am wondering how to leave line breaks in displayed text, I read other threads on stackoverflow on this matter but they weren't helpful, I tried to do something like this:

TextNode tex=TextNode.createFromEncoded(lyrics.text(), lyrics.baseUri());

but I can't get the text I want with line breaks. I looked at previous threads about this like,
Removing HTML entities while preserving line breaks with JSoup
but I can't get the effect I want. What should I do?

Edit: I got the effect I wanted but I don't think it is very good solution:

for (Node nn:listOfNodes)
            {
                String s=Jsoup.parse(nn.toString()).text();
                if ((nn.nodeName()=="#text" || nn.nodeName()=="h3"))
                {
                    buf.append(s+"\n");

                }
            }

Anyone got better idea?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

雨夜星沙 2024-12-25 02:07:11

您可以通过检查节点是否是 TextNode 的实例来获取文本节点(
之间的文本)。这应该适合您:

Document document = Jsoup.connect(url.toString()).get();
Element lyrics = document.select(".lyrics").first();
StringWriter buffer = new StringWriter();
PrintWriter writer = new PrintWriter(buffer);

for (Node node : lyrics.childNodes()) {
    if (node.nodeName().equals("h3")) {
        writer.println(((Element) node).text());
    } else if (node instanceof TextNode) {
        writer.println(((TextNode) node).text());
    }
}

System.out.println(buffer.toString());

(请注意,比较对象的内部值应该通过 equals() 方法完成,而不是 ==;字符串是对象,不是原始类型)

哦,我还建议阅读他们的隐私政策

You could get the text nodes (the text between <br />s) by checking if the node is an instance of TextNode. This should work out for you:

Document document = Jsoup.connect(url.toString()).get();
Element lyrics = document.select(".lyrics").first();
StringWriter buffer = new StringWriter();
PrintWriter writer = new PrintWriter(buffer);

for (Node node : lyrics.childNodes()) {
    if (node.nodeName().equals("h3")) {
        writer.println(((Element) node).text());
    } else if (node instanceof TextNode) {
        writer.println(((TextNode) node).text());
    }
}

System.out.println(buffer.toString());

(please note that comparing the object's internal value should be done by equals() method, not ==; strings are objects, not primitives)

Oh, I also suggest to read their privacy policy.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文