[英]Nothing shows up in the console when scraping a website
我正在做一个个人项目,我想从网站上删除一些游戏排名,但我无法在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);
});
}
});
更改
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
使用错误的索引引起的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.