Here I tried to tell you, why you got the error and how to solve your problem.
require 'nokogiri'
doc = Nokogiri::HTML <<-html
<a ata="1" atb="2" atc="3">Blabla</a>
html
doc.class # => Nokogiri::HTML::Document
doc.respond_to?(:attributes) # => false
doc.at_css('a').class # => Nokogiri::XML::Element
doc.at_css('a').respond_to?(:attributes) # => true
Hash[doc.at_css('a').to_a]
# => {"ata"=>"1", "atb"=>"2", "atc"=>"3"}
Now reason of error undefined method 'attributes' is - your doc
gives a Nokogiri::XML::Document
object, which doesn't respond to attributes
method. attributes
is a method of the Nokogiri::XML::Node
class instances.
Now, there is a multiple way to do this. #at_css
will give us Nokogiri::XML::Node
, where as css
will give you Nokogiri::XML::NodeSet
, which is a collection of Nokogiri::XML::Node
.
Thus, while you want to collect all the elements of a
s attributes. Then you probably do as
below :
doc.css('a').each do |node|
node.each do |attr_name,attr_val|
# work with attr_name,attr_val
end
end
Example :-
doc.at_css('a').each { |att_name, att_value| p [att_name, att_value] }
# >> ["ata", "1"]
# >> ["atb", "2"]
# >> ["atc", "3"]
You can also use the method #attributes
. Just here is an example to show you how it works actually -
node_attributes = doc.at('a').attributes
# => {"ata"=>#(Attr:0x4260fc6 { name = "ata", value = "1" }),
# "atb"=>#(Attr:0x4260fbc { name = "atb", value = "2" }),
# "atc"=>#(Attr:0x4260fb2 { name = "atc", value = "3" })}
node_attributes.do |atr_name,node_attr|
node_attributes[atr_name] = node_attr.content
end
node_attributes # => {"ata"=>"1", "atb"=>"2", "atc"=>"3"}