How do I get the <p> tag parent class name using Nokogiri?-Collection of common programming errors
This is a good use-case for XPath. Here’s how I’d go about it:
require 'nokogiri'
doc = Nokogiri::HTML( #
The $
is a marker in CSS that says that particular part of the selector is what we’re interested in. See “Determining the Subject of a Selector” for more information. Once Nokogiri supports “subjects”, we can streamline our CSS selectors and their accompanying Ruby code because we won’t need to use parent
methods to set up to the parent nodes. Until then, well, we still have the old works around of using parent
.