You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Cheerio.js的.each()搭配.find('a').text()返回单个长字符串问题

问题原因

修复方案

将遍历对象改为<tbody>下的所有<tr>行元素即可,修改后代码如下:

const getTopCollections = async () => {
  const response = await axios.get('https://www.nft-stats.com/top-collections/7d')
  const $ = cheerio.load(response.data)

  const return_data = []

  // 选择表格tbody下的所有tr行作为遍历对象
  const tableRows = $('table tbody tr')
  tableRows.each((i, element) => {
      // 对单一行查找a标签取文本,trim去除首尾空白
      let col_name = $(element).find('a').first().text().trim()
      // 可选过滤空值避免无效条目
      if (col_name) {
        return_data.push(col_name)
      }
  })
  console.log(return_data)
  return return_data
}

内容的提问来源于stack exchange,提问作者burneverything1

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.24 06:36:04