Created
October 19, 2010 20:11
-
-
Save rklemme/635002 to your computer and use it in GitHub Desktop.
Extract piece of an HTML fragment with Nokogiri
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/env ruby19 | |
require 'nokogiri' | |
# Nokogiri should have this as well | |
REPL = { | |
'<' => '<', | |
'≤' => '<=', | |
'>' => '>', | |
'≥' => '>=', | |
'&' => '&', | |
} | |
def REPL.unescape(s) | |
s.gsub /&\w+;/ do |m| | |
self[m] || m | |
end | |
end | |
str="<p>\n <strong>Location:</strong> Columbus, Ohio\n</p>\n\n<" | |
s2 = REPL.unescape(str) | |
# p s2 | |
doc = Nokogiri.HTML s2 | |
puts doc | |
# p doc | |
puts 'elements' | |
doc.xpath('//p[strong]/text()').each do |elm| | |
p elm | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment