Desmond
Desmond

Reputation: 43

Remove empty spaces inside <p> tags using BeautifulSoup

There are some paragraphs in my string html that look like this:

<p>
    Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
</p>

I want to remove empty spaces inside p tags and turn it into:

<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua</p>

Notice that a p tag like this should leave changed:

<p class="has-media media-640"><img alt="Lorem ipsum dolor sit amet" height="357" src="http://www.example.com/img/lorem.jpg" width="636"/></p>

What I want is:

for p in soup.findAll('p'):
    replace p.string with trimmed text

Upvotes: 2

Views: 4149

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1121386

You can replace text with to the element.string.replace_with() method:

for p in soup.find_all('p'):
    if p.string:
        p.string.replace_with(p.string.strip())

Demo:

>>> from bs4 import BeautifulSoup
>>> soup = BeautifulSoup('''\
... <p>
...     Text with whitespace
... </p>
... <p>No whitespace</p>
... <p><span><img /></span></p>
... ''')
>>> for p in soup.find_all('p'):
...     if p.string:
...         p.string.replace_with(p.string.strip())
... 
u'\n    Text with whitespace\n'
u'No whitespace'
>>> print str(soup)
<html><head></head><body><p>Text with whitespace</p>
<p>No whitespace</p>
<p><span><img/></span></p>
</body></html>

This will only strip whitespace directly contained in the tag. If you have other tags contained then no stripping takes place.

You can use the element.strings sequence to handle <p> tags with nested tags in them. I'd not trim all spaces; leave one space around each string if present:

whitespace = u' \t\n\r\x0a'  # extend as needed

for p in soup.find_all('p'):
    for string in list(p.strings):  # copy so we can replace some
        left = string[:1] in whitespace
        right = string[-1:] in whitespace
        if not left and not right:
            continue  # leave be
        new = string
        if left:
            new = ' ' + new.lstrip()
        if right:
            new = new.rstrip() + ' '
        string.replace_with(new)

Demo:

>>> soup = BeautifulSoup('''\
... <p>
...     Text with whitespace
... </p>
... <p>No whitespace</p>
... <p>
...     A nested 
...     <span>tag</span>
...     is not a problem
... </p>
... ''')
>>> whitespace = u' \t\n\r\x0a'  # extend as needed
>>> for p in soup.find_all('p'):
...     for string in list(p.strings):  # copy so we can replace some
...         left = string[:1] in whitespace
...         right = string[-1:] in whitespace
...         if not left and not right:
...             continue  # leave be
...         new = string
...         if left:
...             new = ' ' + new.lstrip()
...         if right:
...             new = new.rstrip() + ' '
...         string.replace_with(new)
... 
u'\n    Text with whitespace\n'
u'\n    A nested \n    '
u'\n    is not a problem\n'
>>> print str(soup)
<html><head></head><body><p> Text with whitespace </p>
<p>No whitespace</p>
<p> A nested <span>tag</span> is not a problem </p>
</body></html>

Upvotes: 6

Related Questions