【问题标题】:Selecting a span within a certain table Cheerio在某个表 Cheerio 中选择一个跨度
【发布时间】:2017-07-25 09:18:07
【问题描述】:

我正在解析一个网站,并且有多个类似这样的表格。

<table class="first tableWithData">
<tbody>
<tr class="first rowTable">
<td class="infoAvail">
    <span class="linking">Saturday</span>
</td>
</tr>
</tbody>
</table>

每个表的表类名都是唯一的。

我想为这个特定表中的每一行提取 Linking 的 span 类,但我正在为使用什么 DOM 选择而苦苦挣扎。

var cheerio = require('cheerio');
var request = require('request');

request('http://testWebsite.com', function (error, response, html) {
  if (!error && response.statusCode == 200) {
    var $ = cheerio.load(html);
    $('table.first tableWithData td span.linking').each(function(i, element)
{
      var a = $(this);
      console.log(a.text());
    });
  }
});

【问题讨论】:

    标签: javascript parsing request cheerio


    【解决方案1】:

    你少了一个点:

    table.first.tableWithData td span.linking
    //         ^
    

    【讨论】:

    • 不确定为什么会有一个点?表名中确实有一个空格。或者,cheerio 就是这样处理空间的?
    • 哦,等一下,我现在明白你的意思了。以后会试试看的!
    • 完美运行,干杯!
    猜你喜欢
    • 1970-01-01
    • 2016-04-23
    • 1970-01-01
    • 1970-01-01
    • 2016-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-06
    相关资源
    最近更新 更多