【问题标题】:How can I detect outer Html tag of key in Html?如何检测 Html 中键的外部 Html 标签?
【发布时间】:2019-11-22 03:10:28
【问题描述】:

我通过改造获取 HTML 页面源并将其保存在字符串中。我正在这个字符串中搜索一个键(产品价格)。我可以在字符串中找到键,我怎样才能找到包含键的外部 html 标记或类?

<input name="productPrice" id="productPrice" value="74.90"
               type="hidden"/>

只需在这个 HTML 标签中,我就可以找到74.90. 以及如何找到 id 或名称?

<div class="newPrice">
    <ins content="3099.00">3.099,00 <span content="TRY">TL</span> 
    </ins><span class="kdv">KDV <br>DAHİL</span>
</div>

这是另一个例子。我可以找到 3099.00。以及如何找到 div 的类名?

希望我能解释一下问题

【问题讨论】:

  • 您能否详细说明一下您最初是如何找到价格的?
  • 我只是使用 indexOf() 函数
  • jsoup 具有出色的文档访问方法,例如您可以将 HTML 文本转换为文档对象,然后使用 document.getElementById("productPrice") 之类的东西来获取元素等。
  • @devnull69 我只知道价格,想知道id。我有不同的html页面。而且每个页面都有不同的id和tags。

标签: android html jsoup retrofit


【解决方案1】:

要获取课程,请选择您所知道的 - 元素 ins,属性为 content,值为 3099.00。然后选择它的父级,得到这个父级的类。

import java.io.IOException;

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

public class JsoupIssue57005455 {

    public static void main(final String[] args) throws IOException {

        String html = "<div class=\"newPrice\">"
                + "    <ins content=\"3099.00\">3.099,00 <span content=\"TRY\">TL</span> "
                + "    </ins><span class=\"kdv\">KDV <br>DAHİL</span>" 
                + "</div>";
        Document doc = Jsoup.parse(html);

        System.out.println(doc.select("ins[content=3099.00]").first().parent().className());
    }
}

输出:newPrice

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-03
    • 2013-12-30
    • 1970-01-01
    • 1970-01-01
    • 2021-07-15
    • 2016-11-26
    相关资源
    最近更新 更多