You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
21 lines
485 B
Python
21 lines
485 B
Python
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 <a> 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()
|