Ruby Nokogiri在标记结束后提取文本

时间:2013-12-04 14:46:24

标签: ruby nokogiri

我在这里有一个相当基本的问题,这意味着我可能会遗漏一些我正在使用Nokogiri来搜索网站的内容。

我想在div中的强标记结束后提取文本,如下所示:

<p style="padding-bottom:0px;"><strong>Location:</strong> Cape Town</p>

目前我的代码如下:

location = detail_page.css('p[style="padding-bottom:0px;"]').text

这显然也提供了<strong>Location:</strong>位,有没有办法在没有的情况下使用正则表达式执行

询问的原因是有相同格式的其他div包含我需要的信息,所以我不能删除强元素。

提前致谢

马克

2 个答案:

答案 0 :(得分:1)

您可以使用XPath:

detail_page.xpath('//p[@style="padding-bottom:0px;"]/strong/following-sibling::text()')

这将选择跟随strong个元素的兄弟姐妹的任何文本节点,这些元素又是p元素的子元素,其style属性的值为padding-bottom:0px;

答案 1 :(得分:0)

我将在此处执行以下操作:

require 'nokogiri'

@doc = Nokogiri::HTML.parse('<p style="padding-bottom:0px;"><strong>Location:</strong> Cape Town</p>')
@doc.at_css('p[style*="padding-bottom:0px;"] > text()').text.strip
# => Cape Town