javascriptx-ray

Two div with the same class name, how can I scrape only the first class instance?


var Xray = require('x-ray');
    var x = Xray();
    var a = false;


    var url = "abcdeabcde";

            x(url, '.listing_risultati_prodotti .smcc-listing-risultati-prodotto', [{
                title: '.first-col  a',
                link: '.product-description a@href',
                prezzo: '.second-col .ish-priceContainer-salePrice'
            }])(function(err, obj) {
                console.log(obj)   
            })

The query right now return two times the same value because there are two div that match the query.

[ { title: 'aaaa',
    link: 'aaaa',
    prezzo: 'aaaa' },
  { title: 'bbb',
    link: 'bbb',
    prezzo: ' bbb' },
  { title: 'ccc',
    link: 'ccc',
    prezzo: 'ccc' },
.....
....
.....
Then again
{ title: 'aaaa',
    link: 'aaaa',
    prezzo: 'aaaa' },
  { title: 'bbb',
    link: 'bbb',
    prezzo: ' bbb' },
  { title: 'ccc',
    link: 'ccc',
    prezzo: 'ccc' }]

both div have same selector path

#maincontent > div.category-section > div.render-category-products.products > div.listing_risultati_prodotti 

They are both nested inside this id: #smcc_comp_common_wrapper

structure is like:

<body>
#smcc_comp_common_wrapper
...
...
#mainwrapper
    ...
    #maincontent > div.category-section > div.render-category-products.products > div.listing_risultati_prodotti 
    ...
#mainwrapper
    ...
    #maincontent > div.category-section > div.render-category-products.products > div.listing_risultati_prodotti 
    ...
...
...

i'm trying something like:

x(url, '.listing_risultati_prodotti:nth-of-type(1) .smcc-listing-risultati-prodotto',

or also

x(url, ':nth-match(1 of #mainwrapper) .listing_risultati_prodotti .smcc-listing-risultati-prodotto', but no one works

Is possible targeting only the first class instance?


Solution

  • it is:

    x(html, 'div#smcc_comp_common_wrapper div#mainwrapper:first-of-type div.listing_risultati_prodotti'
        )(function(err, obj) {
            console.log(obj) 
        })
    

    also there should not be multiple elements with the same id