【发布时间】:2017-04-25 02:19:05
【问题描述】:
我正在尝试使用 Nokogiri 的 CSS 方法从我的 HTML 中获取一些名称。
这是一个 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 中的元素?
【问题讨论】:
-
请阅读“minimal reproducible example”。您的 HTML 无效;请确保结束标签位于正确的位置。没有这些 Nokogiri 会将它们放在它认为应该在的地方,这可能与您的想法大不相同。您的预期输出是什么?