import markdown from bs4 import BeautifulSoup md_input = """This is an image of a tree: ![tree image](tree_img) """ def test_image_replace(): soup = BeautifulSoup(markdown.markdown(md_input), features="html.parser") for img in soup.find_all('img'): img["src"] = "blah" print(soup.prettify())