在 html 代码的 <td> 中搜索特定字符串,如果存在则使用 JSOUP 打印下一个 <td> vaule

Search for particular string in <td> of html code and if present print next <td> vaule using JSOUP

 I have html code like below

<html>
<body>

<div id="1">
    <table>
        <tr>
            <td>ID</td>
            <td>:</td>
            <td>123</td>
        </tr>   

        <tr>
            <td>Status</td>
            <td>:</td>
            <td>Fail</td>
        </tr>
    </table>
</div>
<div id="2">
    <table>
        <tr>
            <td>ID</td>
            <td>:</td>
            <td>456</td>
        </tr>   

        <tr>
            <td>Status</td>
            <td>:</td>
            <td>Success</td>
        </tr>
    </table>
</div>
<div id="3">
    <table>
        <tr>
            <td>ID</td>
            <td>:</td>
            <td>789</td>
        </tr>   

        <tr>
            <td>Status</td>
            <td>:</td>
            <td>Fail</td>
        </tr>
    </table>
</div>
<div id="4">
    <table>
        <tr>
            <td>ID</td>
            <td>:</td>
            <td>135</td>
        </tr>   

        <tr>
            <td>Status</td>
            <td>:</td>
            <td>Success</td>
        </tr>
    </table>
</div>

</body>
</html>

我需要解析这段 HTML 代码。我需要遍历所有存在的 div 标签,并在每个 div 中迭代地搜索 td 中的 "Search"。如果存在,则获取其第二个相邻的 td 值,即失败/成功。如果 If 是 "Fail" 那么我需要再次搜索 "ID" 如果存在我需要打印它的第二个相邻 div 值,即在这种情况下为 123 和 789。

伪代码可能如下所示

if(code contains "Status")
{
    1. Get its 2nd td value i.e., Fail/Success

   if(td value is "Fail")
  {
    1. Search for "ID"
    if("ID" present)
    {
        Print the number/2nd adjacent <td> value    
    }
  }
}

我曾在 java 脚本中尝试过,如下所示

var t0=$(this).find('tr:has(td:contains("Test Status"))');
        if (t0.length) 
        {
            var str0 =t0.text().trim();
            str0 = /:(.+)/.exec(str0)[1];

            if(str0 == "FAIL")
            {

                var t1=$(this).find('tr:has(td:contains("Test ID"))');
                if (t1.length) 
                {
                    str =t1.text().trim();
                    str = /:(.+)/.exec(str)[1];
                    testIDArray.push(str);
                    // alert(str);
                } 
           }

但我需要使用 jsoup 在 java 中完成。我尝试了类似下面的东西

String htmlString = fileContent;
            Document document = Jsoup.parse(htmlString);
            Elements elements = document.body().select("div"); for (Element element : elements) { String link = element.select("td:contains(Test Status)").attr("<tr>");

                 if(link != null || !(link.isEmpty())) 
                 {
                        System.out.println(link);
                        System.out.println("=========================");
                 }
            }

请帮我解决这个问题。我不知道如何进行。

提前致谢。

请帮我解决这个问题。

您可以使用 Java 流来解决这个问题:

List<String> failedIds = document.body().select("div table").stream()
        .map(e -> e.select("tr"))
        .filter(trs -> "FAIL".equalsIgnoreCase(trs.last().select("td").last().text()))
        .map(trs -> trs.first().select("td").last().text())
        .collect(Collectors.toList());

结果将是:

[123, 789]

首先你selectdiv table得到所有的元素。然后你 select 所有 tr 并过滤那些状态为 Fail (trs -> trs.first().select("td").last().text()) 的。最后映射 ID (trs -> trs.first().select("td").last().text()).

要打印 ID 而不是创建列表,您可以使用 .forEach():

document.body().select("div table").stream()
        .map(e -> e.select("tr"))
        .filter(trs -> "FAIL".equalsIgnoreCase(trs.last().select("td").last().text()))
        .map(trs -> trs.first().select("td").last().text())
        .forEach(System.out::println);

或者你可以使用这个(没有流):

for (Element e : document.body().select("div table")) {
    Elements trs = e.select("tr");
    if ("FAIL".equalsIgnoreCase(trs.last().select("td").last().text())) {
        String id = trs.first().select("td").last().text();
        System.out.println(id);
    }
}