첫 번째 h5
항목이 있는지 검색하도록 플래그를 정의 할 수 있습니다. 첫 번째 문자 인 경우 텍스트를 인쇄합니다. 나중에 텍스트를 변수에 저장하여 나중에 사용합니다.
레벨이 h5에서 재설정되면 마지막 노드 텍스트를 작성해야합니다.
마지막으로 노드 텍스트를 써야합니다. 이 같은
뭔가 : 하나가 될 경우이 코드는 H5 텍스트를 반복 할
doc = Nokogiri::HTML(File.read(html))
first_flag = true
last_h5 = nil
doc.each do |node|
#there was a h5 and now we have new version
if first_flag == false and node.name != 'h5'
puts last_h5
first_flag = true
end
case node.name
when 'h1'
puts node.text
when 'h2'
puts node.text
when 'h3'
puts node.text
when 'h4'
puts node.text
when 'h5'
puts node.text if first_flag
first_flag = false
last_h5 = node.text
end
end
#Write the last entry
if first_flag == false and last_h5
puts last_h5
end
. 그러나 귀하의 설명에서 이것은 귀하의 데이터에 해당하지 않습니다.
면책 조항 : 당신은 카운터와 부울 플래그를 교체 할 경우 하나 H5-노드 또한 가장자리 케이스를 잡을 수)
을 테스트되지 않은 코드는 테스트 HTML이 없습니다.
예 :
h5_count = 0
last_h5 = nil
doc.each do |node|
#there was a h5 and now we have new version
if h5_count > 1 and node.name != 'h5'
puts last_h5
h5_count = 0
end
case node.name
when 'h1'
puts node.text
when 'h2'
puts node.text
when 'h3'
puts node.text
when 'h4'
puts node.text
when 'h5'
puts node.text if h5_count == 0
h5_count += 1
last_h5 = node.text
end
end
#Write the last entry
if h5_count > 1
puts last_h5
end
어떤 아이디어를 얻을 수있는 HTML을, 그리고 당신이 정말로 무엇을 원하는가? –
하나의 XPath 표현식으로 원하는 것을 얻을 수 있습니다. –