How to use Python and BeautifulSoup to parse text but include newlines
Solution 1:
Would assume there are already newline characters in your result, but while printing your string, python automatically interprets these characters.
To avoid this behavior you can print your string as representation - repr()
:
print(repr(soup.get_text()))
so you will get:
Hello\n\n\nWorld\n
Example
html = '''
<a>Hello
<br/>
<br/>
World<br/>
'''
soup=BeautifulSoup(html,'lxml')
print(repr(soup.get_text()))
Output
Hello\n\n\nWorld\n