Reputation: 43939
Sample file content:-
<DT><A HREF="http://www.cnn.com/" ADD_DATE="1299885544">CNN</A>
<DT><A HREF="http://www.nytimes.com/" ADD_DATE="1299885544">The New York Times</A>
<DT><A HREF="feed://news.google.com/?output=rss" ADD_DATE="1299885544">Google News</A>
<DT><A HREF="http://www.news.com/" ADD_DATE="1299885544">CNET News.com</A>
<DT><A HREF="http://espn.go.com/" ADD_DATE="1299885544">ESPN</A>
Code i am using:-
path = File.join(directory, bookmark.file_file_name)
file = Nokogiri::HTML(open(path))
count = 1
file.search('//*[@href]').each do |m|
p m
p m[:href]
rescue
next
end
end
O/p for the above code:--
p m
<Nokogiri::XML::Element:0x81dbed64 name="a" attributes=[#<Nokogiri::XML::Attr:0x81dbe120 name="href" value="http://maps.google.com/">, #<Nokogiri::XML::Attr:0x81dbe0e4 name="add_date" value="1299885544">] children=[#<Nokogiri::XML::Text:0x81db3590 "Google Maps">]>
p m[:href]
http://maps.google.com/
I want to have both URL and its value. ie "feed://news.google.com/?output=rss" and "Google News"
Upvotes: 0
Views: 356
Reputation: 5880
m.text will return the value:
h = {} #=> {}
irb(main):021:0> file.search('//*[@href]').each do |m|
irb(main):022:1* h[m[:href]] = m.text
irb(main):023:1> end
=> 0
irb(main):024:0> h
=> {"http://www.cnn.com/"=>"CNN", "http://www.nytimes.com/"=>"The New York Times", "feed://news.google.com/?output=rss"=>"Google News", "http://www.news.com/"=>"CNET News.com", "http://espn.go.com/"=>"ESPN"}
irb(main):025:0>
Upvotes: 1