开发者

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜