两个具有相同类名的div,我怎样才能刮掉第一个类实例?

时间:2021-07-06 19:38:50
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.

查询现在返回两次相同的值,因为有两个div匹配查询。

[ { 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

两个div都有相同的选择器路径

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

They are both nested inside this id: #smcc_comp_common_wrapper

它们都嵌套在这个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

x(url,':nth-​​match(#mainwrapper中的1个).listing_risultati_prodotti .smcc-listing-risultati-prodotto',但没人工作

Is possible targeting only the first class instance?

是否可以只定位第一个类实例?

2 个解决方案

#1


0  

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

也不应该有多个具有相同id的元素

#2


0  

If any solution which uses selectors (e.g. using :first-child as Vaibhav suggested) fails, how about just slicing the resultant object array in half?

如果使用选择器的任何解决方案(例如使用:作为Vaibhav建议的第一个子节点)失败,那么将结果对象数组切成两半怎么样?

function(err, obj) {
    var half = obj.splice(obj.length/2);
}

Note, that it is a kind of a workaround for your problem, however it may be acceptable in that particular case.

请注意,这是一种针对您的问题的解决方法,但在特定情况下可能是可接受的。

#1


0  

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

也不应该有多个具有相同id的元素

#2


0  

If any solution which uses selectors (e.g. using :first-child as Vaibhav suggested) fails, how about just slicing the resultant object array in half?

如果使用选择器的任何解决方案(例如使用:作为Vaibhav建议的第一个子节点)失败,那么将结果对象数组切成两半怎么样?

function(err, obj) {
    var half = obj.splice(obj.length/2);
}

Note, that it is a kind of a workaround for your problem, however it may be acceptable in that particular case.

请注意,这是一种针对您的问题的解决方法,但在特定情况下可能是可接受的。