Reputation: 33755
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.
Upvotes: 1
Views: 2692
Reputation: 66
Please note that Nokogiri treats everything as nodes - be it a text, attribute, or an element. Your document has one child:
irb(main):014:0> my_listing.children.size
=> 1
irb(main):015:0> puts my_listing.children
<p> May 4 - <a href="http://auburn.craigslist.org/web/2996976345.html">residual income No
experience is needed</a> - <font size="-1"> (online)</font> <span class="p">
img</span></p>
=> nil
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:
irb(main):017:0> my_listing.children.first.children.size
=> 6
irb(main):018:0> puts my_listing.children.first.children
May 4 -
<a href="http://auburn.craigslist.org/web/2996976345.html">residual income No
experience is needed</a>
-
<font size="-1"> (online)</font>
<span class="p"> img</span>
=> nil
To get what you asking about, keep going down the hierarchy:
irb(main):022:0> my_listing.children.first.children[0]
=> #<Nokogiri::XML::Text:0x..fd9d1210e " May 4 - ">
irb(main):023:0> my_listing.children.first.children[0].text
=> " May 4 - "
irb(main):024:0> my_listing.children.first.children[1]['href']
=> "http://auburn.craigslist.org/web/2996976345.html"
Upvotes: 2
Reputation: 27222
If I pull down a web page and have an element, as you do:
p c
> => #<Nokogiri::XML::Element:0x3ff9d9c6b660 name="a" ...
you can get the children:
c2 = c.children
and then get their text:
c2.text # or
c2[0].text => => "Watch video! "
The href can get gotten like so:
c["href"] # -> "http://example.com/video/"
Upvotes: 0