使用 nokogiri 打开标签包装的最佳方法是什么?



比如说,我有一个像这样的HTML文件…

<html>
<head>
<title>hello</title>
</head>
<body>
<h1>title</h1>
<p>So, help me <a href="/">remove</a> this.</p>
</body>
</html>

我应该使用什么节点方法来使它像这样?

<html>
<head>
<title>hello</title>
</head>
<body>
<h1>title</h1>
<p>So, help me remove this.</p>
</body>
</html>

可能是…

doc.css('a').each |i|
i.unwrap
end

但是我似乎在文档中找不到。

您可以使用Nokogiri::XML::Node#replace将链接标记替换为其中的文本。

doc.css('a').each do |link|
link.replace(link.inner_html)
end

相关内容

最新更新