在抓取网站时,控制台中没有显示任何内容
我正在做一个个人项目,我想从网站上删除一些游戏排名,但我无法在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
中使用错误的索引引起的。