我正在尝试使用Nokogiri的CSS方法从我的HTML中获取一些名称。使用Nokogiri的CSS方法获取alt标签中的所有元素
这是HTML的一个例子:
<section class="container partner-customer padding-bottom--60">
<div>
<div>
<a id="technologies"></a>
<h4 class="center-align">The Team</h4>
</div>
</div>
<div class="consultant list-across wrap">
<div class="engineering">
<img class="" src="https://v0001.jpg" alt="Person 1"/>
<p>Person 1<br>Founder, Chairman & CTO</p>
</div>
<div class="engineering">
<img class="" src="https://v0002.png" alt="Person 2"/></a>
<p>Person 2<br>Founder, VP of Engineering</p>
</div>
<div class="product">
<img class="" src="https://v0003.jpg" alt="Person 3"/></a>
<p>Person 3<br>Product</p>
</div>
<div class="Human Resources & Admin">
<img class="" src="https://v0004.jpg" alt="Person 4"/></a>
<p>Person 4<br>People & Places</p>
</div>
<div class="alliances">
<img class="" src="https://v0005.jpg" alt="Person 5"/></a>
<p>Person 5<br>VP of Alliances</p>
</div>
我在我的people.rake
文件至今如下:
staff_site = Nokogiri::HTML(open("https://www.website.com/company/team-all"))
all_hands = staff_site.css("div.consultant").map(&:text).map(&:squish)
我有一点点麻烦中的所有元素alt=""
标签(人的名字),因为它嵌套在几个div下。
当前,使用div.consultant
,它获取所有名称+角色,即Person 1Founder, Chairman; CTO
,而不是alt=
中的人名。
我怎么能简单地得到alt
内的元素?
请阅读“[mcve]”。您的HTML无效;请确保结束标签位于正确的位置。如果没有那些Nokogiri会把它们放在它认为应该是的地方,它们可能会与你的想法大相径庭。你的预期产出是多少? –