使用BeautifulSoup </p>删除<p>标记内的空格

时间:2015-02-13 19:49:11

标签: python html beautifulsoup trim

我的字符串html中有一些段落如下所示:

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

我想删除p标记内的空格并将其转换为:

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

请注意,此类p标记应保留更改:

<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>

我想要的是:

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

1 个答案:

答案 0 :(得分:3)

您可以将文字替换为element.string.replace_with() method

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

演示:

>>> 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>

这只会删除标记中直接包含的空白。如果您包含其他标签,则不会进行剥离。

您可以使用element.strings sequence处理带有嵌套标记的<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)

演示:

>>> 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>