如何使用 nokogiri 编辑文件中的所有图像 src 属性?

How to edit all image src attributes from a file using nokogiri?

这就是我现在拥有的。我试图用“/data/content/..”替换我所有 src 属性中的所有“./”。现在我可以单独获取属性并更改它。但是我怎样才能编辑整个对象本身并保存呢?因为我在视图中将它用作字符串对象。

@page = Nokogiri::HTML(@html_content_from_uploaded_rar_index_html)
@page.css('img').each do |node|
 node.each do |attr_name,attr_val|
  attr_val.to_s.gsub("./", "/data/content/")
  // need to save page object with updated src attribute values now
 end
end

谢谢

像这样应该可以解决问题

page.css('img').each do |node|
 node.each do |attr_name,attr_val|
  node.attributes["src"].value = attr_val.to_s.gsub("./", "/data/content/")
 end
end

然后您可以用通常的方式检索更新后的 HTML。