节点.js和带有选择器的 Cheerio 解析表

Node.js and Cheerio parsing table with selectors

本文关键字:Cheerio 选择器 js 节点      更新时间:2023-09-26

我正在尝试使用 Node.js 和 Cheerio 解析 HTML 表并得到一些结果,但不幸的是我得到了太多的数据,不确定如何进一步解析它以仅获取我需要的数据。

这是我到目前为止的一小部分代码。

var request = require("request");
var cheerio = require("cheerio");
request('http://www.myURL.com', function(error, response, body) {
  var $ = cheerio.load(body);
  $('td').each(function() {
    console.log($(this).text());
  });
});

使用 Chrome 插件找到选择器,我发现我需要".clickableRow td",但我尝试插入它的每种方法似乎都不起作用。

为了更清楚一点,html 源代码看起来像这样 -

<html>
 <body>
  <form>
   <table>
    <tbody>
     <td>
      <table class="standardTable">
       <tbody>
        <tr class="clickableRow">
         <td>first thing I want</td>
         <td>second thing I want</td>
         <td>third thing I want</td>
         <td>fourth thing I want</td>

这有意义吗?我想要的项目非常深入到HTML中,我不确定如何达到那个级别。任何帮助将不胜感激!谢谢!

只需使用选择器'.clickableRow td' .