HTML Parser into DOM in Ruby

感情迁移 提交于 2019-12-10 18:55:58

问题


Is there any HTML parser in Ruby that reads HTML document into a DOM Tree and represents HTML tags as DOM elements?

I know Nokogiri but it doesn't parse HTML into DOM tree.


回答1:


Despite your remark, Nokogiri is the way to go:

doc = Nokogiri::HTML('<body><p>Hello, worlds!</body>')

It parses even invalid HTML and returns a DOM tree:

>> doc.class
=> Nokogiri::HTML::Document
>> doc.root.class
=> Nokogiri::XML::Element
>> doc.root.children.class
=> Nokogiri::XML::NodeSet
>> doc.root.children.first.content
=> "Hello, worlds!"


来源:https://stackoverflow.com/questions/13791789/html-parser-into-dom-in-ruby

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!