簡體   English   中英

JSoup解析HTML問題

[英]JSoup Parsing HTML Questions

我正在嘗試使用JSoup來解析一些看起來大致如下的html:

<div class="mod qmy_text withanno">
    <ul class="yfncnhl mytext"></ul>
    <h3>
        <span>Monday, August 12, 1999</span>
    </h3>
    <ul>
        <li><a href="some_link_here">Title of My Article</a><cite>News
                Source<span>(&nbsp;(Sun, Aug 12)</span>
        </cite></li>
    </ul>

我的問題是,如何解析HTML,以便僅返回<cite> -8月12日,星期日。

截至目前,我只能使用以下表達式輸出h3之后的日期

Elements links = doc.select("div[class=mod qmy_text withanno] > h3");
    System.out.println(links.text());

讓您格式化HTML,使其僅包含要查找的元素的路徑。 看起來像

<div class="mod qmy_text withanno">
    ...
    <ul>
        <li>
            ...
            <cite>News Source
                 <span>(&nbsp;(Sun, Aug 12)</span>
            </cite>
        </li>
    </ul>

因此,您的選擇可能看起來像div.mod.qmy_text.withanno > ul > li > cite > span 所以像這樣的代碼

Elements span = doc.select("div.mod.qmy_text.withanno > ul > li > cite > span");
String spanText = span.text();

我們的spanText將包含( (Sun, Aug 12)

如果您只想獲得last ( and last )之間的一部分來獲取Sun, Aug 12您可以使用

String date = spanText.substring(spanText.lastIndexOf('(')+1, spanText.lastIndexOf(')'));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM