from bs4 import BeautifulSoup import markdown with open('/home/lxf/writing/wired/bf-photo-deals.txt', 'r') as f: data = f.read() result = open('/home/lxf/writing/wired/subid-links.txt', 'a') soup = BeautifulSoup(markdown.markdown(data), "lxml") subid = "blackfridayphotodeals2019" page_url = "https://www.wired.com/story/best-black-friday-photography-deals-2019/" for a in soup.find_all('a'): start = a['href'].split('//')[1][:4] if str(start) == 'best' or start == 'goto': l = "%s,,%s,Impact,%s\n" % (page_url, subid, a['href']) result.write(l) result.close() def parse_links(f):