javascript - 关于Jsoup解析html巨大问题!难倒我了,谢谢各位。

WBOY
Release: 2016-06-06 20:38:13
Original
1061 people have browsed it

我现在想解析每个标签里面的的值, 但是现在有个问题就是 有的时候有8个值,有的时候有4个值, 我现在就想解析出每个 内的前四个值,不在乎他有多少值。(注意:tr标签若干)

javascript - 关于Jsoup解析html巨大问题!难倒我了,谢谢各位。

回复内容:

我现在想解析每个标签里面的的值, 但是现在有个问题就是 有的时候有8个值,有的时候有4个值, 我现在就想解析出每个 内的前四个值,不在乎他有多少值。(注意:tr标签若干)

javascript - 关于Jsoup解析html巨大问题!难倒我了,谢谢各位。

按题主给出的html,可以这样:

<code>Document document = Jsoup.parse(html);
Elements elements = document.select("td br:lt(4)");
for (Element element : elements) {
    System.out.println(element.previousSibling());
}
</code>
Copy after login

应题主需求,可以改为:

<code>    Document document = Jsoup.parse(html);
    Elements elements = document.select("td");
    for (Element element : elements) {
        Elements brs = element.select("br:lt(4)");
        if(StringUtils.isNotBlank(brs.toString()))
            for (Element br : brs) {
                System.out.print(br.previousSibling().toString().concat(" "));
            }
        else
            System.out.println(element.html());
    }
</code>
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!