from urllib.request import urlopen from bs4 import BeautifulSoup html = urlopen('https://www.washingtonpost.com/') bs = BeautifulSoup(html, "html.parser") titles = bs.find_all('h2') titles_list = [] #getting the content of tag inside h2 for h2 in titles: titles_list.append(h2.a.text.strip()) # write() argument must be str, not list titles_list_strings = "\n".join(titles_list) text_file = open("results.txt", "w") text_file.write(titles_list_strings) text_file.close()