Skip to content
Advertisement

How do I get a specific elements value that has no unique identifiers with Cheerio

I’m trying to get a specific elements value with Cheerio however I’m unsure how. I’ve looked over the documentation and so far, no dice.

Website example of the value I’m trying to get, specifically the <dd> that holds the number value of player count.

JavaScript

scrape_test is just ensuring that I’m actually receiving data and it does indeed grab the data including the value I need however I don’t know how to refine it to grab specifically what I need.

The response from pNum is undefined, I’ve tried removing .val() in which I then receive 6 array elements however when I try to iterate over them to push them out, I then get another undefined. If I pick a specific position I equally get an undefined.

Advertisement

Answer

For the player count value, you can filter <dt> elements by their text contents, then pull the next element to find the corresponding <dd>:

JavaScript

Another option is the :contains() pseudoselector and the adjacent sibling combinator:

JavaScript

See also Cheerio: How to select element by text content?

cheerio 1.0.0-rc.12

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement