0

First things first, I've looked in a bunch of seemingly related questions that don't directly relate to my problem:

javascript getElementsByClassName from javascript variable

getElementsByClassName doesn't select all my Navigation elements

Javascript: getElementsByClassName not giving all elements

Javascript document.getElementsByClassName not returning all elements

How to change class for all elements retrieved by document.getElementsByClassName

getElementsByClassName vs. jquery

If there is another question that already addresses my specific problem I apologize and please direct me there.

I'm trying to extract opening and current line data from the following page: https://www.sportsbookreview.com/betting-odds/ncaa-basketball/ and it's only returning data for a certain subset of games. The code is below.

convertHalfLines = stringVal => {
  let val
  let halfLine = false
  if (stringVal.substr(-1) === '\u00BD') {
    val = parseFloat(stringVal.slice(0,-1))
    halfLine = true
  } else {
    val = parseFloat(stringVal)
  }
  return halfLine ? val + (Math.sign(val) * 0.5) : val
}

let games = document.getElementsByClassName("_3A-gC")
let gameInfo = Object.keys(games).map(game => {
  let teams = games[game].getElementsByClassName("_3O1Gx")
  let currentLines = games[game].getElementsByClassName("_3h0tU")

  console.log('currentLines',currentLines)

  return {
    'homeTeam': teams[1].innerText,
    'awayTeam': teams[0].innerText,
    'homeWagerPct': parseFloat(currentLines[1].innerText),
    'awayWagerPct': parseFloat(currentLines[0].innerText),
    'homeOpeningLine': convertHalfLines(currentLines[3].getElementsByClassName('_3Nv_7')[0].innerText),
    'awayOpeningLine': convertHalfLines(currentLines[2].getElementsByClassName('_3Nv_7')[0].innerText),
    'homeCurrentLine': convertHalfLines(currentLines[5].getElementsByClassName('_3Nv_7')[0].innerText),
    'awayCurrentLine': convertHalfLines(currentLines[4].getElementsByClassName('_3Nv_7')[0].innerText),
  }
})

The code returns data for a certain set of games, which in and of itself is not consistent. Sometimes it returns data for the first six games, sometimes for the first eight, sometimes less or more than these. Is there something I just don't know about JS that I'm missing or is something else going on?

Jeff
  • 309
  • 4
  • 20
  • Are you making sure this function is called when the page is rendered? Sounds like it could be related to dynamic js loading. How and where is this code called? – mchl18 Nov 26 '18 at 17:23
  • I'm just opening the page and running it in the console. – Jeff Nov 26 '18 at 17:26
  • This just makes it very hard to debug but I feel like a lot of errors happenin due to undefined values. I am trying to refactor this somehow. – mchl18 Nov 26 '18 at 17:45
  • I have once written a very specific crawler for a website using casper, maybe it would be a good idea to do this with a framework: https://github.com/mchl18/casperGrabsch/blob/master/grabsch.js – mchl18 Nov 26 '18 at 17:47
  • I get that there may be errors from undefined values, but what ends up happening is that it eventually doesn't even get the full array of _3Nv_7 items in each game. I'm wondering if maybe it has to do with the fact that a socket connection is opened on the site when the page renders. – Jeff Nov 26 '18 at 19:53
  • The socket connection could indeed be a reason since it changes the DOM heavily. As a workaround you could try saving the HTML and running the script on that – mchl18 Nov 26 '18 at 22:31

0 Answers0