0
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)   
            })

查询现在返回两倍相同的值,因为有两个 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' }]

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

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

它们都嵌套在这个 id 中:#smcc_comp_common_wrapper

结构是这样的:

<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 
    ...
...
...

我正在尝试类似的东西:

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

或者也

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

是否可能仅针对第一类实例?

4

2 回答 2

0

如果任何使用选择器的解决方案(例如,:first-child按照 Vaibhav 的建议使用)失败,那么将生成的对象数组切成两半怎么样?

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

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

于 2016-08-09T19:48:41.537 回答
0

这是:

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

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

于 2016-09-01T14:23:39.460 回答