Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
doc = Nokogiri::HTML(open(url)).xpath("//*") .xpath("//*[br]/text()[string-length(normalize-space()) != 0]") .wrap("<span></span>") puts doc
它只是返回文本......我期待完整的 html 源代码现在包裹在指定的 xpath 元素周围。
尝试
doc = Nokogiri::HTML(open(url)).xpath("//*") .xpath("//*[br and text()[string-length(normalize-space()) != 0]]") .wrap("<span></span>") puts doc
您的 XPath 所做的是获取非空文本节点。根据他们的定义,它们不包含任何标记。
相反,我的 XPath 获取包含至少一个<br>和至少一个非空文本节点的任何节点。
<br>