使用cheerio(JQuery API)解析节点js

时间:2018-05-15 09:07:03

标签: javascript jquery node.js request cheerio

我正在尝试废弃网站。

我想废弃版本(2.5.1),但我不知道如何废弃只有href="<a title=的版本。

<div class="box-title">All Versions</div>

<div class="row" style="margin-left: 2%; margin-right: 2%;">
    <table class="striped" style="margin-left: auto; margin-right: auto;">
        <tbody>
            <tr><td><a title="download Ultimate Ninja Blazing 2.5.1 apk " onclick="ga('send', 'event', 'button', 'download_ver', 'com.bandainamcoent.narutoblazingna_2018-05-08.apk');" href="/download-app/com.bandainamcoent.narutoblazingna/4_com.bandainamcoent.narutoblazingna_2018-05-08.apk/">2.5.1</a></td><td>May 8, 2018</td></tr>
            <tr><td><a title="download Ultimate Ninja Blazing 2.5.0 apk " onclick="ga('send', 'event', 'button', 'download_ver', 'com.bandainamcoent.narutoblazingna_2018-04-30.apk');" href="/download-app/com.bandainamcoent.narutoblazingna/5_com.bandainamcoent.narutoblazingna_2018-04-30.apk/">2.5.0</a></td><td>April 30, 2018</td></tr>
        </tbody>
    </table>
</div>

此外,我想知道如何从我的cheerio

的网站页面获取所有</tr><tr>

结果应如下所示:)

{ version: '2.5.1' },
{ version: '2.5.0' }

我的解析代码:

const row = $('.row');
const version = row.info('tr').find('td').eq(1);

console.log(row);
console.log(version);

const fields = {
    row,
    version
};

感谢大家的帮助:)

1 个答案:

答案 0 :(得分:1)

以下是一个工作示例

我希望这就是你想要的。

&#13;
&#13;
const row = $('.row');
const version = [];

row.find('tr').each(function (i, e) {
  version.push({"version": $(this).find("td a[title^='download']").text()});
});

console.log(version);
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class="box-title">All Versions</div>

<div class="row" style="margin-left: 2%; margin-right: 2%;">
  <table class="striped" style="margin-left: auto; margin-right: auto;">
    <tbody>
      <tr>
        <td><a title="download Ultimate Ninja Blazing 2.5.1 apk " onclick="ga('send', 'event', 'button', 'download_ver', 'com.bandainamcoent.narutoblazingna_2018-05-08.apk');" href="/download-app/com.bandainamcoent.narutoblazingna/4_com.bandainamcoent.narutoblazingna_2018-05-08.apk/">2.5.1</a></td>
        <td>May 8, 2018</td>
      </tr>
      <tr>
        <td><a title="download Ultimate Ninja Blazing 2.5.0 apk " onclick="ga('send', 'event', 'button', 'download_ver', 'com.bandainamcoent.narutoblazingna_2018-04-30.apk');" href="/download-app/com.bandainamcoent.narutoblazingna/5_com.bandainamcoent.narutoblazingna_2018-04-30.apk/">2.5.0</a></td>
        <td>April 30, 2018</td>
      </tr>
    </tbody>
  </table>
</div>
&#13;
&#13;
&#13;