【问题标题】:Nothing shows up in the console when scraping a website抓取网站时控制台中没有显示任何内容
【发布时间】:2025-12-08 05:00:02
【问题描述】:

我正在做一个个人项目,我想从网站上抓取一些游戏排名,但我无法在 HTML 中找到我想要抓取的游戏的标题。

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

request('https://newzoo.com/insights/rankings/top-20-core-pc-games/', (error, response, html) => {
  if (!error && response.statusCode == 200) {
    const $ = cheerio.load(html);


    //var table = $('#ranking');
    //console.log(table.text());
    $('.ranking-row').each((i,el) => {
      const title = $(el).find('td').find('td:nth-child(1)').text();
      console.log(title);
        });
    }

});

【问题讨论】:

    标签: javascript html node.js cheerio


    【解决方案1】:

    改变

    const title = $(el).find('td').find('td:nth-child(1)').text();
    

    const title = $(el).find('td:nth-child(2)').text();
    

    PS:要调试 xpath,请使用 chrome 调试器。如果您转到此特定站点并搜索.ranking-row td td:nth-child(1),您将看到没有返回任何内容。但如果你这样做.ranking-row td:nth-child(2),你会得到想要的结果。 这是一个简单的 xpath 错误,原因是两次查找相同的 td 并在 nth-child 中使用了错误的索引。

    【讨论】: