How to find tags that aren't empty with Nokogiri?
I got this code, but I just want it to grab p-tags that aren't empty, how do I do that?
开发者_StackOverflow社区doc.css('p').first(3).each do |paragraph|
puts raw(paragraph)
end
Use select
:
doc.css('p').select{ |n| n.inner_text }.each do |paragraph|
puts raw(paragraph)
end
If it doesn't matter if you remove the blank tags altogether, you can try something like this:
doc.css('p').each do |node|
node.remove if node.inner_text == ''
end
Not very elegant, but add this before your code and you won't get any blank nodes in subsequent queries.
精彩评论