Instead of just assigning a new (string) value to the link variable name, you need to change the element itself, in this case by setting its src attribute:
new_src = link.replace('foo', 'bar') # or element.get('src').replace('foo', 'bar') element.set('src', new_src)
Please note: if you know which โlinksโ you are interested in, for example, only img tags, you can also get elements using .findall() (or xpath or css selectors) instead of using .iterlinks() .
source share