nokogiri选择带文字匹配的段落

时间:2016-02-06 22:45:12

标签: ruby nokogiri scraper open-uri

所以我写了一个刮刀,我试图只得到包含 On Snow Feel

的段落文本

我试图将其拉出来,但我不确定如何让nokogiri拉出具有匹配文字的段落。

目前我有boards[:onthesnowfeel] = html.css(".reviewfold p").text ,但这会抓住所有段落。并且不要假设段落将始终按顺序排列。所以不能做[2]或其他什么。

但是你会使用什么方法来刮取与“On Snow Feel”文本相匹配的段落

<div id="review" class="reviewfold">
<p>The <strong>Salomon A</strong><b>assassin</b>&nbsp;Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. </p>
<p><b>Approximate Weight</b>: Moew mix is pretty normal</p>
<p><strong>On Snow Feel:&nbsp;</strong>At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum.</p>
<p><strong>Powder:&nbsp;</strong>It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. </p>
</div>

1 个答案:

答案 0 :(得分:1)

您可以将Enumerable#find与正则表达式匹配=~结合使用,以获得所需的元素内容。

html.css(".reviewfold p").find { |e| e.text =~ /On Snow Feel/ }.text