代码之家  ›  专栏  ›  技术社区  ›  Nicolas Albarracin

如何与Cheerio精确匹配

  •  0
  • Nicolas Albarracin  · 技术社区  · 6 年前

    我试图对一个网页的各个li's做一个片段,但是我遇到了一个问题,因为我正在寻找每个部分的标题来保存不同子对象的数据标题“Caractersticas generales”和“Caractersticas”将发生冲突,第一个“一般特征”是工作正常,但另一个是得到自己的李加上从组成的标题。

            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 回复  |  直到 6 年前
        1
  •  0
  •   pguardiario    6 年前

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