我在Rails中遇到了一些與Nokogiri有關的奇怪問題。我試圖刪除一個「爲什麼」類的「p」標籤。我有以下的代碼,它不工作:無法刪除Nokogiri中的節點
def test_grab
f = File.open("public/test.html")
@doc = Nokogiri::HTML.parse(f)
f.close
@doc = @doc.css("p")
@doc.each do |p|
if p["class"] == "why"
logger.info p.values
p.remove
end
end
end
的test.html:
<html>
<head>
<title>Test</title>
</head>
<body>
<p>Test data</p>
<p>More <a href="http://stackoverflow.com">Test Data</a></p>
<p class="why">Why is this still here?</p>
</body>
</html>
輸出HTML源代碼:
<p>Test data</p>
<p>More <a href="http://stackoverflow.com">Test Data</a></p>
<p class="why">Why is this still here?</p>
我知道Rails代碼即將進入如果循環因爲logger.info出現在服務器終端上。
任何想法?
_why的隱藏參考? :D:D – 2010-12-03 10:24:35