Created
November 5, 2017 16:34
-
-
Save koduki/88b14187ce9033c5155cf5e5ed0064d3 to your computer and use it in GitHub Desktop.
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
require 'nokogiri' | |
def f node | |
node.children.map do |x| | |
[x.name, x.children.size, f(x)] | |
end | |
end | |
def nodes2text(nodes, level) | |
nodes.map { |n| | |
"#{"\t"*level}#{n[0]}, #{n[1]}" + nodes2text(n[2], level+1) | |
}.reduce(""){|r, x| | |
r += "\n" + x | |
} | |
end | |
html = Nokogiri::HTML(open('hoge02.htm'));1 | |
nodes = f html;1 | |
text = nodes2text(nodes, 0) | |
open('hoge2.txt', 'w'){|f| f.puts text} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment