如果另一列包含目标词,则从一个表列中收集数据

时间:2019-08-07 03:21:18

标签: javascript regex web-scraping

我想使用javascript / jQuery搜索html表。

如果其中一个单元格包含一个将总是在第五列中的词,那么我希望从第一列开始输入日期。

我附上了要搜索的表格的示例。

我想我必须在桌子上做一个正则表达式,然后捕获包含文本的单元格?

  @if($_SERVER['REQUEST_URI']==='/dashboard/inventory')
        <link href="{{ url('css/authenticated.css?d='. $date) }} " id="styles" rel="stylesheet">
        <link href="{{ url('css/template/style.css?d='. $date)}}" rel="stylesheet">
    @endif

1 个答案:

答案 0 :(得分:0)

您可以使用以下选择器tr选择所有'#dgNoticeAudit tbody tr',然后从那里开始提取单元格。然后,您可以使用.filterString.includes过滤包含所描述单词的数据。

let rows = document.querySelectorAll('#dgNoticeAudit tbody tr');
var data = Array.prototype.map.call(rows, tr => {
    let td = tr.cells[4]; // index 4 for the 5th column
    return td.innerHTML || td.innerText || '';
});

console.log(data);
/* result will be = [
    "An externally printed NFD document has been imported.",
    "Note inserted",
    "Cancelled task with Id: TF04563335 - Task type: Enforcement Review - Letter↵",
    "Closed task with Id: TF04563335 - Task type: Enforcement Review - Letter↵",
    "Closed task with Id: TF04562943 - Task type: Enforcement Review Applications↵",
    "Challenge logged, reason code: E_SPCIRCUM",
    "Release hold",
    "Hold info modified - CORROHOLD - Upto Mar 5 2019 ",
    "Created task with Id: TF04563335 - Task type: Enforcement Review - Letter↵",
    "Hold all actions - CORROHOLD - Upto Mar 4 2019 "
]; */

    const words = 'Closed task'; // Your searching words
    let filteredData = data.filter(sentence => sentence.includes(words));
    console.log(filteredData);
    /*result will be = [
        "Closed task with Id: TF04563335 - Task type: Enforcement Review - Letter↵",
        "Closed task with Id: TF04562943 - Task type: Enforcement Review Applications↵",
    ]*/