我正在解析 HTML 内容:
doc = Nokogiri::HTML::DocumentFragment.parse(html_content)
我正在用类名“pubdate online”替换 p 标签中的文本。
doc.css("p.pubdate.online").each do |node|
node.content = self.send("#{article_type}_released_at").strftime("%B %d, %Y")
end
我正在尝试将解析后的 HTML 转换回原始 HTML。
doc.to_html
但现在 doc.to_html
没有 <DOCTYPE!>
或 <head>
标签或 <body>
标签。所以问题是在使用 HTML 时我得到的是原始 HTML 代码而不是 HTML 页面。帮助我将文档转换回带有替换文本的原始 HTML 内容。
最佳答案
如前所述here ,你能试试这个吗:
node = doc.at_css("p.pubdate.online")
node.content = self.send("#{article_type}_released_at").strftime("%B %d, %Y")
doc.to_html
关于html - 在 Ruby 中转换回 HTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41690855/