简体   繁体   English

Jsoup HTML解析查询

[英]Jsoup Html parsing query

im a newb with Jsoup, i would like parse this code: 我用Jsoup创建了一个newb,我想解析以下代码:

<span class="vi-tm-left">
                <span>(27 apr 2018</span>
                <span class="endedDate">19:17:55 CEST)</span>
</span>

in order to get : 为了得到 :

27 apr 2018 19:17:55 CEST 2018年4月27日19:17:55 CEST

any tips? 有小费吗?

Assuming that you have selected the document in the doc variable, and that the vi-tm-left class is unique: 假设您已在doc变量中选择了文档,并且vi-tm-left类是唯一的:

final String dateWithBrackets = doc.select("span.vi-tm-left").first().text();
final String date = dateWithBrackets.substring(1, dateWithBrackets.length() - 2);

An example to get it: 一个例子来得到它:

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

public class JsoupMain {

    public static void main(String[] args) {
        String html = "<span class=\"vi-tm-left\"><span>(27 apr 2018</span><span class=\"endedDate\">19:17:55 CEST)</span></span>";

        Document doc = Jsoup.parse(html);
        String text = doc.select("span.vi-tm-left").text().replace("(", "").replace(")", "");
        System.out.println(text);
    }

}

Do like this: 这样做:

        String html = "<html><head><title>First parse</title></head>"
                + "<body><p>Parsed HTML into a doc.</p></body></html>";
        Document doc = Jsoup.parse(html);
        Element firstSpan = doc.select("span.vi-tm-left").first().text();
        Element secondSpan = doc.select("span.vi-tm-left").get(1).text();

        String result = firstSpan.text() + secondSpan.text();
        result = result.substring(1, dateWithBrackets.length() - 2);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM