BeautifulSoup, get_text(), splitlines(), how to remove empty lines in pythonic one liner?

Viewed 1321

Is there a one liner where I can get the text from the soup object, then use splitlines to get a list of each line in the html. Then remove all the excess empty lines in the list that just have newline in it.

I don't want to write another for loop to pass thru twice and clean up the new lines. Also, any other pythonic way to do this is appreciated.

soup = BeautifulSoup('myhtml.html', 'html.parser')
sections = soup.findAll(div, class_='section')
lines = []
for section in sections:
    lines = lines + section.get_text().splitlines()
2 Answers
Related