最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

在抓取网站时,控制台中没有显示任何内容

IT培训 admin 7浏览 0评论

在抓取网站时,控制台中没有显示任何内容

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

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

request('/', (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);
        });
    }

});
回答如下:

更改

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中使用错误的索引引起的。

在抓取网站时,控制台中没有显示任何内容

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

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

request('/', (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);
        });
    }

});
回答如下:

更改

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中使用错误的索引引起的。

发布评论

评论列表 (0)

  1. 暂无评论