简体   繁体   中英

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.

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. If you go to this specific site and search for .ranking-row td td:nth-child(1) , you will see that nothing is returned. But if you do .ranking-row td:nth-child(2) you would get the desired result. This is a simple xpath error caused by looking for the same td twice and using the wrong index in nth-child .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM