[英]Nothing shows up in the console when scraping a website
I'm doing a personal project where I want to scrape some game rankings off a website, but I'm unable to locate in the HTML the titles of the games that I want to scrape. 我正在做一个个人项目,我想从网站上删除一些游戏排名,但我无法在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);
});
}
});
Change 更改
const title = $(el).find('td').find('td:nth-child(1)').text();
to 至
const title = $(el).find('td:nth-child(2)').text();
PS: To debug xpaths, use the chrome debugger. PS:要调试xpath,请使用chrome调试器。 If you go to this specific site and search for
.ranking-row td td:nth-child(1)
, you will see that nothing is returned. 如果您访问此特定站点并搜索
.ranking-row td td:nth-child(1)
,您将看到没有返回任何内容。 But if you do .ranking-row td:nth-child(2)
you would get the desired result. 但是如果你做
.ranking-row td:nth-child(2)
你会得到想要的结果。 This is a simple xpath error caused by looking for the same td
twice and using the wrong index in nth-child
. 这是一个简单的xpath错误,它是通过两次查找相同的
td
并在nth-child
使用错误的索引引起的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.