Home How to edit all image src attributes from a file using nokogiri?
Reply: 1

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

Chet
1#
Chet Published in 2018-02-14 06:27:52Z

This is what I have right now. Im trying to replace all of the "./" in all my src attributes with "/data/content/.." . Now im able do individually get the attributes and change it. but how can I edit the entire object itself and save it? as im using it as a string object in the view.

@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

Thanks

Anthony L
2#
Anthony L Reply to 2018-02-14 06:36:29Z

Something like this should do the trick

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

You can then retrieve the updated HTML in the usual way.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.299268 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO