import sys from bs4 import BeautifulSoup html_doc = open(sys.argv[1], mode="r") html = html_doc.read() soup = BeautifulSoup(html, features="lxml") for link in soup.find_all("a"): if "href" in link.attrs.keys(): if (link['href'].startswith("#") and "tabindex" not in link.attrs.keys() and "target" not in link.attrs.keys()): link['target'] = "_self" html_doc.close() html_doc = open(sys.argv[1], mode="w") print(soup, file=html_doc) print("File written")