Possible Duplicate:
Get link and href text from html doc with Nokogiri & Ruby?
I'm getting the following Nokogiri output:
obj = [#<Nokogiri::XML::Element:0x19b1418 name="a" attributes=[#<Nokogiri::XML::Attr:0x123dd44 name="href" value="http://sample.com">] children=[#<Nokogiri::XML::Element:0x123c408 name="span" attributes=[#<Nokogiri::XML::Attr:0x1201f24 name="class" value="highlight">] children=[#<Nokogiri::XML::Text:0x1143b64 "Web">]>, #<Nokogiri::XML::Text:0x113a9c4 "Sample Text">]>]
How can I get the value "http://sample.com"
? I tried obj.attributes("value")
but had no luck.
I'm using:
You're almost done:
require 'nokogiri'
doc = Nokogiri::HTML(<<EOT)
<a href="http://sample.com">
<span class="highlight">Web</span>
</a>
Sample text
EOT
obj = doc.search('a')
obj.first['href']
=> "http://sample.com"
If there's only one <a>
tag in the document, you could simplify the code using at
:
obj = doc.at('a')['href']
would return the same value.