I crawled a page and stored elements from the page into an array.
If I inspect the first element:
puts "The inspection of the first my_listing: "
puts my_listing.first.first.inspect
The output is:
The inspection of the first my_listing:
#<Nokogiri::XML::Element:0x80c58764 name="p" children=[#<Nokogiri::XML::Text:0x80c584e4 " May 4 - ">, #<Nokogiri::XML::Element:0x80c58494 name="a" attributes=[#<Nokogiri::XML::Attr:0x80c58340 name="href" value="http://auburn.craigslist.org/web/2996976345.html">] children=[#<Nokogiri::XML::Text:0x80c57f08 "residual income No experience is needed!!!">]>, #<Nokogiri::XML::Text:0x80c57da0 " - ">, #<Nokogiri::XML::Element:0x80c57d50 name="font" attributes=[#<Nokogiri::XML::Attr:0x80c57bfc name="size" value="-1">] children=[#<Nokogiri::XML::Text:0x80c577c4 " (online)">]>, #<Nokogiri::XML::Text:0x80c5765c " ">, #<Nokogiri::XML::Element:0x80c5760c name="span" attributes=[#<Nokogiri::XML::Attr:0x80c574b8 name="class" value="p">] children=[#<Nokogiri::XML::Text:0x80c57080 " img">]>]>
How do I access each element? For instance, how do I access the first Text element in this object which would be ‘May 4 – ‘?
If I do:
puts my_listing.first.first.text,
I get this output:
May 4 - residual income No experience is needed!!! - (online) img
Also, how do I access the href attribute?
my_listing.first.first[:href]
which does not work.
Please note that Nokogiri treats everything as nodes – be it a text, attribute, or an element. Your document has one child:
By the way, puts uses to_s method, and that method assembles texts from all children – this is why you see more text than you want.
If you go deeper to see the children of that single element, you have:
To get what you asking about, keep going down the hierarchy: