1

I'm practicing creating an API by scraping using cheerio. I'm scraping from this fairly convoluted site: http://www.vegasinsider.com/nfl/odds/las-vegas/

I'm trying to target the text after these <br> tags within the anchor tag in this <td> element:

<td class="viCellBg1 cellTextNorm cellBorderL1 center_text nowrap" 
      width="56">
   <a class="cellTextNorm" href="/nfl/odds/las-vegas/line-movement/packers-@- 
       bears.cfm/date/9-05-19/time/2020#BT" target="_blank">
        &nbsp;<br>46u-10<br>-3½&nbsp;-10
   </a>
 </td>

The code below is what i'm using to target the data I want. The problem I'm having is I don't know how to get that text after the <br> tags. I've tried .find('br') and couldn't get it to work. Here is the code:

app.get("/nfl", function(req, res) {
  var results = [];

  axios.get("http://www.vegasinsider.com/nfl/odds/las-vegas/").then(function(response) {
    var $ = cheerio.load(response.data);

    $('span.cellTextHot').each(function(i,element) {
      // console.log($(element).text());
      var newObj = {
        time:$(element).text()
      }
      $(element).parent().children().each(function(i,thing){
        if(i===2){
          newObj.awayTeam = $(thing).text();
        }
        else if (i===4){
          newObj.homeTeam = $(thing).text();
        }
      });
      newObj.odds= $(element).parent().next().next().text().trim();
      $('.frodds-data-tbl').find('td').next().next().children().each(function(o, oddsThing){
        if(o===0){
          newObj.oddsThing = $(oddsThing).html();
        }
      });
    res.json(results);
  });
});

You can see I am able to output all the text in this box to the newObj.odds value. I was trying to use something like the next line where I'm targeting that td element and loop through and break out each row into its own newObj property, newObj.oddsLine1 and newObj.oddsLine2 for example.

Hope that makes sense. Any help is greatly appreciated.

kenneth2k1
  • 53
  • 11
  • Does this answer your question? [How do I get text after single
    tag in Cheerio](https://stackoverflow.com/questions/74418220/how-do-i-get-text-after-single-br-tag-in-cheerio)
    – ggorlen Sep 01 '23 at 22:53

1 Answers1

2

You can't select text nodes with cheerio, you need to use js dom properties / functions:

$('td a br')[0].nextSibling.nodeValue

Note $(css)[0] will give you the first element as a js object (rather than a cheerio object)

pguardiario
  • 53,827
  • 19
  • 119
  • 159
  • Thank you, that definitely targets each individual row I'm looking for. My next question would be let's say I wanted to target just the values in the "VI Consensus" column of that page? http://www.vegasinsider.com/nfl/odds/las-vegas/ I was thinking of doing a loop since the row position would be consistent regardless of the amount of rows, but I also thought that seems overly-complicated and there are way smarter brains out there on this. – kenneth2k1 Jun 09 '19 at 23:28
  • Looks like `$('.cellTextNorm:nth-child(3)').map((i, el) => $(el).text()).get()` – pguardiario Jun 09 '19 at 23:42
  • How can I parse nodeValue like I would with text()? – A.com Jul 03 '20 at 15:55
  • nodeValue is a string. You can parse it with regex I guess. – pguardiario Jul 04 '20 at 00:24