-3

I am trying to scrape a player count on a website but my code keeps returning empty. What did I do wrong?

I'm trying to scrape this website: https://www.game-state.com/193.203.39.13:7777/ and the value I want to console.log is Players: x/1000

setInterval(function() {
  var url = 'https://www.game-state.com/193.203.39.13:7777/';
  request(url, function(err, response, body) {
    var $ = cheerio.load(body);
    var players = $('.value #players');
    var playersText = players.text();
    console.log(playersText);
  })
}, 300)
CherryDT
  • 13,941
  • 2
  • 31
  • 50
GhOsT_x
  • 35
  • 5
  • https://www.freecodecamp.org/news/the-ultimate-guide-to-web-scraping-with-node-js-daa2027dcd3/ – Kemy Apr 01 '20 at 12:12

1 Answers1

1

Your selector is wrong.

You are selecting any tag with ID players that is inside of a tag with class value. What you want is both checks on the same element. The issue is the space between .value and #players - remove it: .value#players.

A tip: Try it in your browser first... go to the page, open devtools, enter document.querySelector('.value #players') - you'll see it comes back empty. With .value#players it works.

But, since an ID is supposed to be unique, just the ID #players would suffice anyway.

CherryDT
  • 13,941
  • 2
  • 31
  • 50