I'm trying to parse a HTML document using the BeautifulSoup Python library, but the structure is getting distorted by <br>
tags. Let me just give you an example.
Input HTML:
<div>
some text <br>
<span> some more text </span> <br>
<span> and more text </span>
</div>
HTML that BeautifulSoup interprets:
<div>
some text
<br>
<span> some more text </span>
<br>
<span> and more text </span>
</br>
</br>
</div>
In the source, the spans could be considered siblings. After parsing (using the default parser), the spans are suddenly no longer siblings, as the br tags became part of the structure.
The solution I can think of to solve this is to strip the <br>
tags altogether, before pouring the html into Beautifulsoup, but that doesn't seem very elegant, as it requires me to change the input. What's a better way to solve this?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…