This is my code:
doc= Nokogiri::HTML(open("http://www.cincinnatisun.com/index.php?rss/90d24f4ad98a2793", 'User-Agent' => 'ruby'))
search=doc.css('item')
if !search.blank?
search.each do |data|
title=data.css("title").text
link=data.css("link").text
end
end
but I did not get the link.
Several things are wrong:
won’t work because
searchwould be a NodeSet returned bydoc.css.NodeSet‘s don’t have ablank?method. Perhaps you meantempty??isn’t the correct way to find the
titlebecause, like in the above problem, you’re getting a NodeSet instead of a Node. Gettingtextfrom a NodeSet can return a lot of garbage you don’t want. Instead do:Changing the code to this:
Outputs:
The
linkwon’t work because the XML is malformed, which, in my experience, is unbelievably common on the internet because people don’t take the time to check their work.The fix is going to take massaging the XML prior to Nokogiri receiving the content, or to modify your accessors. Luckily, this particular XML is easy to work around so this should help:
Which outputs:
All that done, you can write your code more clearly like:
Interestingly enough, now the sample page appears to have its links fixed.