How do I remove a node with Nokogiri?

前端 未结 2 627
面向向阳花
面向向阳花 2020-12-14 05:14

How can I remove tags using Nokogiri?

I have the following code but it wont work:

# str = \'

        
2条回答
  •  太阳男子
    2020-12-14 06:21

    I prefer CSS over XPath, as it's usually much more readable. Switching to CSS:

    require 'nokogiri'
    
    doc = Nokogiri::HTML('')
    

    After parsing the document looks like:

    doc.to_html
    # => "\n\n\n\n"
    

    Removing the tags:

    doc.search('img').each do |src|
      src.remove
    end
    

    Results in:

    doc.to_html
    # => "\n\n"
    

提交回复
热议问题