【问题标题】:How to get Text from Parent div only using Jsoup in Android如何仅在 Android 中使用 Jsoup 从父 div 获取文本
【发布时间】:2016-05-25 21:34:53
【问题描述】:

我正在与Android Application 合作,使用Jsoup 获取来自websiteextracting 文本,在下面的Html 中我只想获取parent div 的文本。我想要的是显示Date & Time,它位于class "fr"parent div 中。

<div class="fr">
        <div id="newssource">
            <a href="http://nhl.com" class="newssourcelink" target="_blank">
                Philadelphia Flyers
            </a>
        </div>
        April 15, 2014, 11:13 a.m.
    </div>

我尝试过的。

for(Element detailsDate:document.getElementsByClass("fr")){
    newsDate.add(detailsDate.clone().children().remove().last().text().trim());
}

它只从child div 获取文本,即"Philadelphia Flyers",它位于"a" tag,但我只想显示Date &amp; Time

【问题讨论】:

  • 有人知道解决办法吗?

标签: android jquery html css


【解决方案1】:

使用下面的 jQuery 代码仅获取日期和时间表单“fr”div

<script>
 jQuery('.fr').each(function(){
  var textValue = jQuery(this).clone()    //clone the element
    .children() //select all the children
    .remove()   //remove all the children
    .end()  //again go back to selected element
    .text();
  alert(textValue);
});
</script>

【讨论】:

  • 我需要 java 代码而不是 JavaScript 我正在使用 android jsoup,正如我在问题中提到的那样
  • .end() 在使用 Jsoup 的 Java 中不受支持,所以我使用 .last() 作为替代方案,我的代码与您在答案中提到的相同,但它仍然不能获取子文本父母的
【解决方案2】:

我自己找到了答案。刚刚在这里发布给其他有同样问题的人。

for(Element detailsDate:document.getElementsByClass("fr")){
    newsDate.add(detailsDate.getElementById("newssource").nextElementSibling());
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-05
    • 2012-03-14
    • 2018-06-06
    • 2016-02-15
    相关资源
    最近更新 更多