我的任务是获取没有数据的文档的HTML结构。来源:使用Nokogiri获取HTML结构
<html>
<head>
<title>Hello!</title>
</head>
<body id="uniq">
<h1>Hello World!</h1>
</body>
</html>
我想:
<html>
<head>
<title></title>
</head>
<body id="uniq">
<h1></h1>
</body>
</html>
有多种方式与引入nokogiri提取数据,但我无法找到一种方法执行相反的任务。
UPDATE: 找到解决的办法是我收到的两个答案的组合:
doc = Nokogiri::HTML(open("test.html"))
doc.at_css("html").traverse do |node|
if node.text?
node.remove
end
end
puts doc
输出正是一个我想要的。
可能重复[?如何创建使用引入nokogiri页面上的HTML代码结构的轮廓(http://stackoverflow.com/questions/7176094/how- do-i-create-an-html-tag-structure-on-the-page-using-nokogiri) – Phrogz