如何用< p>包装HTML未加标签的文本使用Nokogiri标记? [英] How do I wrap HTML untagged text with <p> tag using Nokogiri?
本文介绍了如何用< p>包装HTML未加标签的文本使用Nokogiri标记?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我必须将HTML文档解析为不同的新文件.问题是有些文本节点没有用"<p>"
标签包裹,而是在每个段落的末尾带有"<br>"
标签.
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 "<p>"
tags, instead they having "<br>"
tags at the end of each paragraph.
我想使用Nokogiri用<p>
标签包裹此文本:
I want to wrap this text with <p>
tags using Nokogiri:
<div id="f15"><b>Footnote 15</b>: Catullus iii, 12.</div>
<div class="pgmonospaced pgheader"><br/>
<br/>
End of the Project abc<br/>
<br/>
*** END OF THIS PROJECT XYZ ***<br/>
<br/>
***** This file should be named new file.html... *****<br/>
<br/></div>
推荐答案
在一些论坛中进行搜索并在本地进行了一些调试之后,我发现了以下解决问题的方法.
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
这篇关于如何用< p>包装HTML未加标签的文本使用Nokogiri标记?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文