matthewmueller / x-ray

The next web scraper. See through the <html> noise.
MIT License
5.87k stars 349 forks source link

how can get second item of array with x-ray #180

Closed salehmontazeran closed 5 years ago

salehmontazeran commented 8 years ago

i want's to get the second item in the game: ['.contenedor-numero'], array :


  var site = 'http://www.laliga.es/en/';
  var url = 'liga-bbva';
  var address = site + url;
  x(address, '#div_clasf_38_1_3 table tbody tr', [{
      rank: ".posicion",
      game:  ['.contenedor-numero'],
      score: ".contenedor-numero.puntos",
      name: x(".contenedor-nombre a", {
          Abbreviation: '.nombre-equipo-clasificacion-movil',
          complete: '.nombre-equipo-clasificacion'
      }),

  }])(function(err, data) {
      console.log(data);
  });

the html code struct is this :

<tr class=" ">
    <td class="posicion">1</td>
    <td class="contenedor-flecha"></td>
    <td class="contenedor-nombre">
        <a href="http://www.laliga.es/en/liga-bbva/barcelona">
            <span class="escudo-equipo-clasificacion">
                            <span class="sprite-escudos-xs barcelona"></span>
            </span>
            <span class="nombre-equipo-clasificacion">FC Barcelona</span>
            <span class="nombre-equipo-clasificacion-movil">FCB</span>
        </a>
    </td>
    <td class="contenedor-numero puntos">91</td>
    <td class="contenedor-numero ">38</td>
    <td class="contenedor-numero no-sidebar">29</td>
    <td class="contenedor-numero no-sidebar">4</td>
    <td class="contenedor-numero no-sidebar">5</td>
    <td class="contenedor-numero no-sidebar">112</td>
    <td class="contenedor-numero no-sidebar">29</td>
</tr>

I want's to scraping td elements that has class="contenedor-numero " with value of 38 ... but when i use ['.contenedor-numero'][1] nothing give me !!!

how can i get second elemt of that array ???

mcornella commented 8 years ago

You have to use CSS nth-of-type:

{
  ...
  game:  '.contenedor-numero:nth-of-type(2)',
  ...
}

Another option is leaving it as an array (game: ['.contenedor-numero']) and then inside the function get just the second item in the array:

}])(function(err, data) {
  if (err) return console.log(err);

  data.forEach(function(item) {
    item.game = item.game[1];
  });

  return data;
});

['.contenedor-numero'][1] doesn't work because you're defining the array and getting its 2nd element at the same time. Since there is no 2nd element because it's an array of just 1 element, you get undefined. You can test that in the developer console for example.

BTW don't leave out the if (err) return console.log(err) of the ending function, it will save you of many headaches.