java - 使用正则表达式或 xpath 从包含特定文本的 html 中提取 <td> 值

标签 java regex xpath jmeter

我正在使用 JMeter,我想从包含特定值的行的响应中提取列的值:

具体来说,我想获取包含 677777 的行,并为该行获取列 dv-col dv-col-type-enum dv-col-task-state 在此特定情况下为打开

尝试了几个正则表达式但还没有成功,所以欢迎任何帮助或线索。

<!DOCTYPE html>
<html lang="en">
    <head></head>
    <body class="bootstrap env-dlt">
        <div id="main-container" class="container-fluid">
            <div id="main-header" class="header"></div>
            <div class="section">
                <div class="dv-filters large-content"></div>
                <div class="dv-container large-content">
                    <table class="table table-striped table-bordered table-condensed">
                        <thead></thead>
                        <tbody class="taskList">
                            <tr>..</tr>
                            <tr>..</tr>
                            <tr>..</tr>
                            <tr>
                                <td class="dv-col dv-col-type-string dv-col-task-panel"></td>
                                <td class="dv-col dv-col-type-string dv-col-task-phase"></td>
                                <td class="dv-col dv-col-type-long dv-col-task-proposal_reference">
                                    <a href="/presentation/workflow/tasks/111111/detail.html">
                                        677777
                                    </a>
                                </td>
                                <td class="dv-col dv-col-type-long dv-col-task-proposal_acronym"></td>
                                <td class="dv-col dv-col-type-enum dv-col-task-state">
                                    Open
                                </td>
                                <td class="dv-col dv-col-type-long dv-col-task-owner"></td>
                                <td class="dv-col dv-col-type-date dv-col-task-deadline"></td>
                                <td class="dv-col dv-col-type-double dv-col-task-score"></td>
                                <td class="dv-col dv-col-type-action"></td>
                            </tr>
                            <tr>..</tr>
                            <tr>..</tr>
                            <tr>..</tr>

Xpath 可能是替代方案?

谢谢

最佳答案

获取期望结果的xpath是:

//tr[td/a[contains(text(),"677777")]]/td[@class="dv-col dv-col-type-enum dv-col-task-state"]/text()

首先找到<tr>包含 <td>它有一个 <a>的 text() 包含,或等于 677777,然后找到 <td> class 选项卡是目标下的“dv-col dv-col-type-enum dv-col-task-state”<tr>

关于java - 使用正则表达式或 xpath 从包含特定文本的 html 中提取 <td> 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28836485/

相关文章:

regex - Jenkins 认为正则表达式

python - scrapy xpath 如何

java - 无法检查字符串是否为回文

java - kubernetes 中的 Grpc 连接不起作用 => 不可用 : Network closed for unknown reason

java - 执行update返回1但不更新

python - Lxml html xpath 上下文

xml - XPATH 按位置选择返回不连贯的结果

java - 检查输入空

python - 正则表达式只返回匹配的第一部分

c# - 正则表达式替换 - 多个字符