Beautiful soup ignore multiple spaces out of tags

Viewed 122

when I use BeautifulSoup to get the text from html, I find it will ignore multiple spaces. For the example below, there are 2 spaces between </seg> and <seg>, but output only have one space, and whatever how many spaces, it'll sill output one space.

import bs4

text = "<line><seg>aaa</seg>  <seg>bbb</seg></line>"
soup = bs4.BeautifulSoup(text)
print(soup.text)
print(soup.find_all(text=True))

output is:

aaa bbb
['aaa', ' ', 'bbb']

but what I really want is:

aaa  bbb
['aaa', '  ', 'bbb']

Any ideas?

or

if there is an equivalent way in javascript? to get text but ignore multiple spaces outside of tag

1 Answers

That is normal behaviour for html parsers.

See:

https://developer.mozilla.org/en-US/docs/Web/API/Document_Object_Model/Whitespace

To quote some relevant parts:

HTML largely ignores whitespace?

In the case of HTML, whitespace is largely ignored — whitespace in between words is treated as a single character, and whitespace at the start and end of elements and outside elements is ignored.

Creating space around and inside elements is the job of CSS.

What does happen to whitespace?

They don't just disappear, however.

Any whitespace characters that are outside of HTML elements in the original document are represented in the DOM. This is needed internally so that the editor can preserve formatting of documents. This means that:

There will be some text nodes that contain only whitespace, and Some text nodes will have whitespace at the beginning or end.

How does CSS process whitespace?

Most whitespace characters are ignored, not all of them are.....There are rules in the browser engine that decide which whitespace characters are useful and which aren’t — these are specified at least in part in CSS Text Module Level 3, and especially the parts about the CSS white-space property and whitespace processing details.

Related