0

I'm trying to use cheerio to webscape a sports website . One of the things that I need is data-gameid from this line

<div class="component game scoreboard-game margin-24-bottom epl full time  margin-24-bottom" data-gameid="2146609" data-league="epl" data-show-date="" data-extra-class="margin-24-bottom" data-viewport-width="">

I really just can't figure out how to get the data-gameid number. I know its a short question but i don't know what else to say to make it more specific

2 Answers 2

1

Use jQuery's .data method like so:

$(".game").data("gameid");

Or use .attr:

$(".game").attr("data-gameid");

Or for a pure JavaScript solution:

document.querySelector(".game").getAttribute("data-gameid");
Sign up to request clarification or add additional context in comments.

1 Comment

Wouldn't ".game[data-gameid]" be better suited as your CSS selector? Then you can ensure that if you do get back an element with the game class, it will also have a gameid data attribute, thereby preventing any issues where that attribute isn't defined.
0

In case you want to get it from the DOM tree simply use the .attr method.

const gameId = $('.game').attr('data-gameid');

If you're talking about extracting it from that string do the following.

const nodeStr = `<div class="component game scoreboard-game margin-24-bottom epl full time  margin-24-bottom" data-gameid="2146609" data-league="epl" data-show-date="" data-extra-class="margin-24-bottom" data-viewport-width="">`;
const gameId = /data-gameid="(\d+)"/mgi.exec(nodeStr)[1]; // "2146609"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.