开发者

How do I create an outline of the HTML tag structure on the page using Nokogiri?

I am trying to create an outline of the tag structure of an HTML page using Nokogiri that I can use as an indicator whether an html page's content has changed.

To do t开发者_开发百科his, basically I want to strip all the text out, and just have the HTML tags remaining (without attributes).

The idea is to use this as a sketch of the page, one of a few I use, to see if the page has changed.

When I'm done, I want the "sketch" to look roughly like

<html><head></head><body><div></div><p><div></div></p></body></html>

So that it can be compared against revisions to see if the page structure has changed.

There are a ton of examples of how to parse the dom in Nokogiri. But, how about just listing it?

Any thoughts anyone?


Something like this would do:

class Nokogiri::XML::Node

  def to_sketch
    children.find_all(&:element?).map(&:to_sketch).join
  end
end

class Nokogiri::XML::Element
  def to_sketch
    "<#{name}>#{super}</#{name}>"
  end
end

EDIT An example

require 'nokogiri'
require 'open-uri'
Nokogiri::HTML(open('http://google.com')).to_sketch

Returns:

"<html><head><meta></meta><title></title><script></script><style></style><script></script></head><body><textarea></textarea><div><div><nobr><b></b><a></a><a></a><a></a><a></a><a></a><a></a><a><u></u></a></nobr></div><div><nobr><span></span><span></span><span><a></a></span><a></a><a></a></nobr></div><div></div><div></div></div><center><br></br><div><a><img></img></a><br></br><br></br></div><form><table><tr><td></td><td><input></input><input></input><input></input><div><input></input></div><br></br><span><span><input></input></span></span><span><span><input></input></span></span></td><td><a></a><a></a></td></tr></table></form><div><br></br><div><font><a></a><a></a><a></a></font><br></br><br></br></div></div><div></div><span><center><div><div><a></a><a></a><a></a><a></a></div></div><p><a></a></p></center></span><div></div><div><script></script></div><script></script><script></script></center></body></html>"
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜