s = '<html><body><p><img src="old_value"/>some text</p></body></html>'
tree = etree.HTML(s)
imgs = tree.xpath('.//img')
thm = 'new-value'
for img in imgs:
    img.set('src', thm)
a = etree.Element('a', href='#')
img.addprevious(a)
a.insert(0, img)
a.tail = img.tail
img.tail = ''
etree.tostring(tree)
lxml python load html string without header and body and add element around targeted elements