加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 如何使用Nokogiri导航DOM

发布时间:2020-12-17 04:19:36 所属栏目:百科 来源:网络整理
导读:我正在尝试填充变量parent_element_h1和parent_element_h2.任何人都可以帮我使用 Nokogiri来获取我需要的信息到这些变量中吗? require 'rubygems'require 'nokogiri'value = Nokogiri::HTML.parse(-HTML_END) "html body p id='para-1'A/p div class='block
我正在尝试填充变量parent_element_h1和parent_element_h2.任何人都可以帮我使用 Nokogiri来获取我需要的信息到这些变量中吗?
require 'rubygems'
require 'nokogiri'

value = Nokogiri::HTML.parse(<<-HTML_END)
  "<html>
    <body>
      <p id='para-1'>A</p>
      <div class='block' id='X1'>
        <h1>Foo</h1>
        <p id='para-2'>B</p>
      </div>
      <p id='para-3'>C</p>
      <h2>Bar</h2>
      <p id='para-4'>D</p>
      <p id='para-5'>E</p>
      <div class='block' id='X2'>
        <p id='para-6'>F</p>
      </div>
    </body>
  </html>"
HTML_END

parent = value.css('body').first

# start_here is given: A Nokogiri::XML::Element of the <div> with the id 'X2
start_here = parent.at('div.block#X2')

# this should be a Nokogiri::XML::Element of the nearest,previous h1.
# in this example it's the one with the value 'Foo'
parent_element_h1 = 

# this should be a Nokogiri::XML::Element of the nearest,previous h2. 
# in this example it's the one with the value 'Bar'
parent_element_h2 =

请注意:start_here元素可以位于文档内的任何位置. HTML数据只是一个例子.也就是说,标题< h1>和< h2>可能是start_here的兄弟姐妹,也可能是start_here的兄弟姐妹.

以下递归方法是一个很好的起点,但它不适用于< h1>因为它是start_here兄弟的孩子:

def search_element(_block,_style)
  unless _block.nil?
    if _block.name == _style
      return _block
    else
      search_element(_block.previous,_style)
    end
  else
    return false
  end
end

parent_element_h1 = search_element(start_here,'h1')
parent_element_h2 = search_element(start_here,'h2')

在接受了答案之后,我想出了my own solution.它就像一个魅力,我觉得它非常酷.

解决方法

我觉得这几年来得太晚了,但觉得有必要发帖,因为所有其他解决方案都太复杂了.

这是XPath的一个声明:

start = doc.at('div.block#X2')

start.at_xpath('(preceding-sibling::h1 | preceding-sibling::*//h1)[last()]')
#=> <h2>Foo</h2>    

start.at_xpath('(preceding-sibling::h2 | preceding-sibling::*//h2)[last()]')
#=> <h2>Bar</h2>

这可以容纳直接的兄弟姐妹或以前的兄弟姐妹的孩子.无论哪一个匹配,last()谓词都可以确保您获得最接近的先前匹配.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读