如何在 Cheerio 中换行 p 标签?

How can I line break p tag in Cheerio?

我正在从网站上抓取一些段落,我遇到了这个问题,但我不知道如何解决它。

结构是这样的,例如:

<div class = "container">
   <p> This is a long paragraph 1. </p>
   <p> This is a long paragraph 2. </p>
   <p> This is a long paragraph 3. </p>
   <p> This is a long paragrahp 4. </p>
</div>

所以我做了类似的事情来获取我刚才提到的示例段落中的文本。

function scrapeData() {
    let data = []
    let url = `scraping-url`;
    axios(url)
    .then(response =>{
        const html = response.data
        const $ = cheerio.load(html, {xmlMode: true})

        $('.container', html).each(function(){
            const text = $(this).find('p').text()
            data.push({
              text
            })
            console.log(data)
        })

    }).catch(err => console.log(err))
}

但我得到的结果是{This is a long paragraph 1.This is a long paragraph 2.This is a long paragraph 3.This is a long paragraph 4.}粘在一起,我想把这些段落分成每个文本块

我想要这样在我的 console.log(data)

{
    This is a long paragraph 1.
    This is a long paragraph 2.
    This is a long paragraph 3.
    This is a long paragraph 4.
}

调整选择器以匹配 p 标签,然后遍历每个标签并构建您的数据。

试试这个:

   // select p tags in the container
    $('.container p', html).each(function(){
        const text = $(this).text();
        data.push({
          text
        });
    });

    console.log(data);

也许在之后添加换行符:

$('p').after("\n")

或者当你加入他们时:

$('p').get().map(p => $(p).text()).join("\n")