2017-08-11 77 views
1

使用這個語法:使用相同的className選擇多個標籤?

x('http://www.viadeo.com/fr/company/unicef', 
    '.page-content', 
    [{ 
    img:'[email protected]', 
    bio:'.pan-desc-description', 

    org:'.pan-desc-footer-element @element-value', 
    link: '.element-value [email protected]', 
    **twitter:'.element-value [email protected]'** // I get the previous link not the twitter one 

}]).write('result.json') 

有與特定的類名的網站中的多個項目,但它只返回第一個。有沒有辦法抓住他們所有的人,也許我可以做一個限制與回報?我很抱歉,如果它在文檔中,我已經讀過兩遍,看起來它沒有明確說出任何地方。

回答

1

您可以利用Chrome檢查工具來獲得正確的選擇,

這裏,該代碼爲我工作,

var Xray = require('x-ray'); 
var x = Xray(); 
x('http://www.viadeo.com/fr/company/unicef', 
'.page-content', 
[{ 
    img:'[email protected]', 
    bio:'.pan-desc-description', 
    org:'.pan-desc-footer-element @element-value', 
    link: '.element-value [email protected]', 
    twitter:'.mbs:nth-child(4) [email protected]' // or use div.element-value.gu.gu-last [email protected] 
}]).write('result.json') 

在那裏,我們得到這樣的結果。

[ 
    { 
    "img": "http://static8.viadeo-static.com/fzv6VNzGukb7mt5oV0Nl-wQxCDI=/fit-in/200x200/filters:fill(white)/7766b960b98f4e85affdab7ffa9863c7/1434471183.jpeg", 
    "bio": "Le Fonds des Nations unies pour l'enfance (abrégé en UNICEF ou Unicef pour United Nations International Children's Emergency Fund en anglais) est une agence de l'ONU consacrée à l'amélioration et à la promotion de la condition des enfants. Son nom était originellement United Nations International Children's Emergency Fund, dont elle a conservé l'acronyme. Elle a activement participé à la rédaction, la conception et la promotion de la convention relative aux droits de l'enfant (CIDE), adoptée suite au sommet de New York en 1989. Son revenu total en 2006 a été de 2 781 millions Dollar US.\r\n   L'UNICEF a reçu le prix Nobel de la paix en 1965.", 
    "link": "http://www.unicef.org/", 
    "twitter": "http://www.twitter.com/UNICEF " 
    } 
] 

這裏是你如何能在Chrome得到適當的選擇:

首先右鍵單擊,然後單擊檢查。 enter image description here

然後,您單擊複製選擇器,然後使用它。 enter image description here

當您複製的選擇,它會這樣說,

#pan-desc > div.pan-desc-grey > div > div:nth-child(4) > div.element-value.gu.gu-last > a 

您可以直接使用它,或者完善它。

+0

謝謝你從我的內心深處,現在我更瞭解它的工作原理! – Taieb

1

你只需要用括號來包裝你想要的「分割」。

這是我工作的代碼。

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

x('http://www.viadeo.com/fr/company/unicef', 
    '.page-content', 
    { 
    img:'[email protected]', 
    bio:'.pan-desc-description', 

    org:'.pan-desc-footer-element @element-value', 
    link: ['.element-value [email protected]'], 
    //twitter:'.element-value [email protected]' 

}) 
(function(err, title) { 
    console.log(title) 
}); 

我還刪除了外括號,因爲頁面內容永遠不會有多個元素,所以你永遠不會想要多於一個。

相關問題