How do I wrap HTML untagged text with

tag using Nokogiri?

前端 未结 1 821
忘掉有多难
忘掉有多难 2021-01-06 07:28

I have to parse an HTML document into different new files. The problem is that there are text nodes which have not been wrapped with \"

\" tags, instead

相关标签:
1条回答
  • 2021-01-06 08:26

    After searching around some forums and doing some debugging locally, i have found the following solution to my problem.

    html_doc = Nokogiri::HTML.parse('path/to/html_file')
    html_doc
    html_doc.search("//br/preceding-sibling::text()|//br/following-sibling::text()").each do |node|
        node.replace(Nokogiri.make("<p>#{node.to_html}</p>"))
    end
    
    0 讨论(0)
提交回复
热议问题