Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 我如何搜索“如何搜索”;“文本”;然后从找到的节点遍历DOM?_Ruby_Nokogiri - Fatal编程技术网

Ruby 我如何搜索“如何搜索”;“文本”;然后从找到的节点遍历DOM?

Ruby 我如何搜索“如何搜索”;“文本”;然后从找到的节点遍历DOM?,ruby,nokogiri,Ruby,Nokogiri,我有一个网页,我需要从中获取一些数据。问题是,每个页面可能有也可能没有特定的数据,或者在DOM中它的上方或下方可能有额外的数据,并且没有CSS ID可言 通常,我可以使用CSS ID或XPath来访问我要查找的节点。在这种情况下,我没有这个选择。我要做的是搜索“标签”文本,然后在下一个节点中获取数据: <tr> <td><b>Name:</b></td> <td>Joe Smith <small&g

我有一个网页,我需要从中获取一些数据。问题是,每个页面可能有也可能没有特定的数据,或者在DOM中它的上方或下方可能有额外的数据,并且没有CSS ID可言

通常,我可以使用CSS ID或XPath来访问我要查找的节点。在这种情况下,我没有这个选择。我要做的是搜索“标签”文本,然后在下一个
节点中获取数据:

<tr> 
    <td><b>Name:</b></td> 
    <td>Joe Smith <small><a href="/Joe"><img src="/joe.png"></a></small></td> 
</tr>
在我需要的数据之前获取节点,但我不确定如何从那里导航。

可能是您正在寻找的方法

require 'nokogiri'

data = File.read "html.htm"

doc  = Nokogiri::HTML data

els  = doc.search "[text()*='Name:']"
el   = els.first

puts "Found element:"
puts el
puts

puts "Parent element:"
puts el.parent
puts

puts "Parent's next_element():"
puts el.parent.next_element

# Output:
#
# Found element:
# <b>Name:</b>
#
# Parent element:
# <td> 
#     <b>Name:</b>
# </td>
#
# Parent's next_element():
# <td>Joe Smith <small><a href="/Joe"><img src="/joe.png"></a></small>
# </td>
require 'nokogiri'

html = '
<html>
  <body>
    <p>foo</p>
    this text
    <p>bar</p>
  </body>
</html>
'

doc = Nokogiri::HTML(html)
doc.at('p:contains("foo")').next_sibling.text.strip
=> "this text"
需要“nokogiri”
data=File.read“html.htm”
doc=Nokogiri::HTML数据
els=doc.search“[text()*='Name:']”
el=els.first
放置“找到的元素:”
普茨埃尔
放
放置“父元素:”
把el.parent
放
放置“父元素的下一个元素():”
放置el.parent.next_元素
#输出:
#
#找到的元素:
#姓名:
#
#父元素:
#  
#姓名:
# 
#
#父元素的下一个元素()
#乔·史密斯
# 
请注意,由于文本位于
标记内,因此在到达下一个同级元素之前,必须先向上一级(到找到的元素的父级
)。如果HTML结构不稳定,您必须找到第一个父级,即
,然后从那里开始。

需要“nokogiri”
require 'nokogiri'

html = '
<html>
  <body>
    <p>foo</p>
    this text
    <p>bar</p>
  </body>
</html>
'

doc = Nokogiri::HTML(html)
doc.at('p:contains("foo")').next_sibling.text.strip
=> "this text"
html='1〕 福

本文 酒吧

' doc=Nokogiri::HTML(HTML) doc.at('p:contains(“foo”)).next_sibling.text.strip =>“此文本”
您可以使用xpath的父/同级语法在一条语句中完成整个搜索:

>> require 'nokogiri' 
=> true   
>> html = <<HTML
<tr> 
    <td><b>Name:</b></td> 
    <td>Joe Smith <small><a href="/Joe"><img src="/joe.png"></a></small></td> 
</tr>
HTML
>> doc = Nokogiri::HTML(html)

>> doc.at_xpath("//*[text()='Name:']/../following-sibling::*").to_s
=> "<td>Joe Smith <small><a href=\"/Joe\"><img src=\"/joe.png\"></a></small>\n</td>"
>需要“nokogiri”
=>正确
>>html=doc=Nokogiri::html(html)
>>doc.at_xpath(“//*[text()='Name:']/../following sibling::*”)到
=>“乔·史密斯\n”

语义标记。。。多稀有的鸟啊…:(@Nick Faraday:我不明白:为什么在这种情况下不能使用CSS或XPath?在我自己尝试了你的示例并无意中省略了一些代码后,我通过在
els
el
末尾调用
.class
来了解返回的内容。