Help with nokogiri

24 views
Skip to first unread message

Gm

unread,
Jun 3, 2015, 8:44:00 AM6/3/15
to rubyonra...@googlegroups.com
I'm trying to work with nokogiri but I have some problems
I need to iterate a string that has several tags <img> with a base64 image and change it for a url

example:

"<p>test</ p><img src=\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgA..." alt = \ "\"> bla bla bla <img src=\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgA..." alt = \ "\">"

Change to:

"<p>test</ p><img src=\"http:www.blablabla.com" alt = \ "\"> bla bla bla <img src=\"http:www.blablabla.com" alt = \ "\">"

How can I select these img tags, change the src content and return the same string modified ?

Thanks.

Gm

unread,
Jun 3, 2015, 9:40:44 AM6/3/15
to rubyonra...@googlegroups.com
Well, I managed to this:

    doc = Nokogiri::HTML::DocumentFragment.parse(string)
    doc.css('img').map { |i| i.set_attribute(:src, "http://www.blabla.com") }
    p doc.to_s
Reply all
Reply to author
Forward
0 new messages