gpt4 book ai didi

javascript - 选择具有相同类名的多个标签?

转载 作者:行者123 更新时间:2023-11-30 15:06:40 25 4
gpt4 key购买 nike

使用这种语法:

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

org:'.pan-desc-footer-element @element-value',
link: '.element-value a@href',
**twitter:'.element-value a@href'** // I get the previous link not the twitter one

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

网站中有多个具有该特定类名的项目,但它只返回第一个。有没有办法捕获所有这些,也许我可以用那个返回做一个 .limit?如果它在文档中,我深表歉意,我已经通读了两遍,看起来好像没有在任何地方明确说明。

最佳答案

您可以利用 chrome 检查器工具来获得合适的选择器,

在这里,这段代码对我有用,

var Xray = require('x-ray');
var x = Xray();
x('http://www.viadeo.com/fr/company/unicef',
'.page-content',
[{
img:'img@src',
bio:'.pan-desc-description',
org:'.pan-desc-footer-element @element-value',
link: '.element-value a@href',
twitter:'.mbs:nth-child(4) a@href' // or use div.element-value.gu.gu-last a@href
}]).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

可以直接使用,也可以提炼。

关于javascript - 选择具有相同类名的多个标签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45643510/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com