matthewmueller / x-ray

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

Selecting all the elements of a html #309

Closed iCrackk closed 5 years ago

iCrackk commented 6 years ago

So I want to select all the 'p span' element of the html that looks similar to this:

This is the first one

This is the second one

This is the third one

and this is what my code looks like: const xray = require('x-ray'); const x = xray();

x(url,'p span')(function(err,product){ if(err == null) console.log(product); else console.log(err); })

but the problem is that it only displays the first element that is this: "This is the first one"

So is there anything that I'm missing? I want to select and display all of the 'p span' elements in this case it would look like:

This is the first one

  <p><span>This is the second one</span></p>
  <p><span>This is the third one</span></p>

Any help would be much appreciated.

levibuzolic commented 6 years ago

Please read the documentation about collections. This should get things working:

x(url, {
  products: ['p span']
})(function(err, data){
  if(err == null) console.log(data);
  else console.log(err);
})