开发者

How to find tags that aren't empty with Nokogiri?

开发者 https://www.devze.com 2023-01-28 05:38 出处:网络
I got this code, but I just want it to grab p-tags that aren\'t empty, how do I do that? 开发者_StackOverflow社区

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

精彩评论

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