如何与Cheerio完全匹配

时间:2018-12-15 20:44:34

标签: javascript node.js cheerio

我正在尝试对网页的各种内容进行剪贴,但是遇到了一个问题,因为我正在寻找每个部分的标题以将数据保存在不同的子对象标题“Característicasgenerales”和“ Características”即将发生冲突,第一个“Característicasgenerales”工作正常,但另一个正在从组合标题中获得自己的li加上那些。

        const characteristicsHelper = $("[class='general-section article-section']");
        characteristicsHelper.each((index, element) => {
            //test2.push($(element, '.section-title > h4').text());
            if (($(element).find('h4:Contains("Características generales")').text())) {
                ($(element).find('li').each((index, data) => {
                    dwelling.generalCharacteristics.push($(data).text());
                }));
            }
            if (($(element).find('h4:Contains("Servicios")').text())) {
                ($(element).find('li').each((index, data) => {
                    dwelling.services.push($(data).text());
                }));
            }
            if (($(element).find('h4:Contains("Ambientes")').text())) {
                ($(element).find('li').each((index, data) => {
                    dwelling.environments.push($(data).text());
                }));
            }
            if (($(element).find('h4:Contains("Características")').text())) {
                ($(element).find('li').each((index, data) => {
                    dwelling.characteristics.push($(data).text());
                }));
            }
        });

1 个答案:

答案 0 :(得分:0)

您可以使用过滤器:

$(element).find('h4').filter(h4 => $(h4).text() === "Características")