在Ruby中将DOM解析器转换为DOM [英] HTML Parser into DOM in Ruby
本文介绍了在Ruby中将DOM解析器转换为DOM的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我知道Nokogiri,但它不解析HTML到DOM树中。
解决方案
尽管您有这样的评论,但Nokogiri仍然可以:
doc = Nokogiri :: HTML('< body>< p> Hello,worlds!< / body>')
$ c它解析甚至无效的HTML并返回一个DOM树:
>> doc.class
=> Nokogiri :: HTML :: Document
>> doc.root.class
=> Nokogiri :: XML :: Element
>> doc.root.children.class
=> Nokogiri :: XML :: NodeSet
>> doc.root.children.first.content
=> 你好,世界!
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.
解决方案 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!"
这篇关于在Ruby中将DOM解析器转换为DOM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文