使用相同的className选择多个标签?
问题描述:
使用这个语法:使用相同的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')
有与特定的类名的网站中的多个项目,但它只返回第一个。有没有办法抓住他们所有的人,也许我可以做一个限制与回报?我很抱歉,如果它在文档中,我已经读过两遍,看起来它没有明确说出任何地方。
答
您可以利用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得到适当的选择:
当您复制的选择,它会这样说,
#pan-desc > div.pan-desc-grey > div > div:nth-child(4) > div.element-value.gu.gu-last > a
您可以直接使用它,或者完善它。
答
你只需要用括号来包装你想要的“分割”。
这是我工作的代码。
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)
});
我还删除了外括号,因为页面内容永远不会有多个元素,所以你永远不会想要多于一个。
谢谢你从我的内心深处,现在我更了解它的工作原理! – Taieb