How do I wrap HTML untagged text with

tag using Nokogiri?

前端 未结 1 741
忘掉有多难
忘掉有多难 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("

    #{node.to_html}

    ")) end

    0 讨论(0)
提交回复
热议问题