如何使用Nokogiri解析此HTML代码?

如何使用Nokogiri解析此HTML代码?,html,ruby,html-parsing,nokogiri,Html,Ruby,Html Parsing,Nokogiri,我有以下HTML: <h3><strong>Adresse:</strong></h3> <p> Hochschule Darmstadt<br> TechnologieTransferCentrum<br> D19, Raum 221, 222<br> Schöfferstraße 10<br> <b>64295 Darmstadt</b><p>

我有以下HTML:

<h3><strong>Adresse:</strong></h3>
    <p>
Hochschule Darmstadt<br>
TechnologieTransferCentrum<br>
D19, Raum 221, 222<br>
Schöfferstraße 10<br>
<b>64295 Darmstadt</b><p>
<h3>Kommunikationsdaten: </h3> 
<p>

从这个基础出发,

# encoding: UTF-8
require 'nokogiri'

doc = Nokogiri::HTML(<<EOT)
<h3><strong>Adresse:</strong></h3>
    <p>
Hochschule Darmstadt<br>
TechnologieTransferCentrum<br>
D19, Raum 221, 222<br>
Schöfferstraße 10<br>
<b>64295 Darmstadt</b><p>
<h3>Kommunikationsdaten: </h3> 
<p>
EOT

puts doc.errors
puts doc.to_html
由此产生:

<h3><strong>Adresse:</strong></h3>
    <p>
Hochschule Darmstadt<br>
TechnologieTransferCentrum<br>
D19, Raum 221, 222<br>
Schöfferstraße 10<br><b>64295 Darmstadt</b></p><p>
</p><h3>Kommunikationsdaten: </h3>
<p></p>
或:

需要两个
next_sibling
方法。第一个查找紧跟在
节点末尾的文本节点:

doc.at('h3').next_sibling
=> #<Nokogiri::XML::Text:0x3fef59dedfb8 "\n    ">
doc.at('h3')。下一个兄弟姐妹
=> #

假设您已在
doc
中解析文档,则:

puts doc.at('//h3[contains(strong, "Adresse:")]/following-sibling::p').text
将为您提供以下输出:

Hochschule Darmstadt
技术转移中心
D19,劳姆221222
舍弗斯特拉10
64295达姆施塔特

你到底在问什么,结束标记应该放在哪里?比如当你解析html.css('h1')时,它会在它们之间获取信息,但是这些代码没有结束标记,如何获取信息你计划如何识别合适的
?可以安全地假设它是
中唯一的一个,或者您需要通过它的名称(“ADRESE”)来识别它吗?
doc.at('p').text
=> "\nHochschule Darmstadt\nTechnologieTransferCentrum\nD19, Raum 221, 222\nSchöfferstraße 1064295 Darmstadt"
doc.at('h3').next_sibling.next_sibling.text
=> "\nHochschule Darmstadt\nTechnologieTransferCentrum\nD19, Raum 221, 222\nSchöfferstraße 1064295 Darmstadt"
doc.at('h3').next_sibling
=> #<Nokogiri::XML::Text:0x3fef59dedfb8 "\n    ">
puts doc.at('//h3[contains(strong, "Adresse:")]/following-sibling::p').text