【问题标题】:cheerio accessing a class within a divCheerio 访问 div 中的类
【发布时间】:2020-04-08 21:59:26
【问题描述】:

好的,所以我正在尝试从 https://www.jamesqquick.com/blog 抓取信息。这是我的功能代码。

这里一切正常。

const axios = require('axios');
const cheerio = require('cheerio');

axios.get('https://www.jamesqquick.com/blog').then( res => {
        const talks = [];
        const $ = cheerio.load(res.data);

        $('li.card').each( (index, element) => {
        const title = 
            $(element)
            .children()
            .first()
            .text();

        const sildesLink = $(element)
            .children('a')
            .last()
            .attr('href');

        const date = $(element)
            .children('small')
            .last()
            .text();
        talks[index] = {title, sildesLink, date}
        })

        console.log(talks);
    })

我正在尝试从 card--descriptionpost--tag 中抓取每篇博文的描述。这是我正在访问的卡片的 HTML。

<li class="card">
<a href="/blog/react-router-in-five-minutes">
<h3 class="card--title">React Router in 5 Minutes</h3>
</a>
<small class="card--date">04/06/2020</small>
<div>
<p class="card--description">I use React all of the time and always have to look up how to setup React Router. In this post, I'll show you how to setup React Router in…</p>
<small>Tags:<!-- --> 
<small class="post--tag">web-development</small>
<small class="post--tag">javascript</small>
<small class="post--tag"> react</small>
</small>
</div>
</li>

如何访问卡片--描述post--tag。这是我如何做的当前代码。

        $('.card--description').each( (index, element) => {
            const desc = $(element)
            .children()
            .first()
            .text();
            test[index] = {desc}
            })

结果是这样的。

[
  { desc: '' }, { desc: '' },
  { desc: '' }, { desc: '' },
  { desc: '' }, { desc: '' },
  { desc: '' }, { desc: '' },
  { desc: '' }, { desc: '' },
  { desc: '' }, { desc: '' },
  { desc: '' }, { desc: '' },
  { desc: '' }
]

所以它正在拾取一些东西。任何帮助,将不胜感激。此代码的预期结果是能够显示所有说明。

【问题讨论】:

  • 听起来您已经拥有了解决此问题所需的所有工具:只需先查看 in $('.card--description') 中的内容(例如控制台记录它或其他内容),然后弄清楚如何根据您实际找到的数据获取您想要的数据?
  • 是的,每当我在控制台记录变量时,它只会让我清空一个对象
  • 好的,我有标签。来自 .siblings().text()
  • 改用test.push($(element).text());.children() 获取不是文本节点的子节点。
  • 女士们先生们,这里是 .first().text()

标签: javascript html axios cheerio


【解决方案1】:

您可以简单地使用 className 和 tagName 作为目标

$('li p.card--description').each((index, element) => {
    const desc = $(element).text();
    test[index] = { desc }
})
$('li small.post--tag').each((index, element) => {
    const desc = $(element).text();
    test2[index] = { desc }
})

【讨论】:

    【解决方案2】:

    我会这样做:

    $('li.card').get().map(li => {
      return {
        cardDescription: $(li).find('.card--description').text(),
        postTags: $(li).find('.post--tag').get().map(small => $(small).text())
      }
    })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-04
      • 1970-01-01
      • 2020-04-02
      • 2019-01-02
      • 1970-01-01
      相关资源
      最近更新 更多