Python Beautiful Soup - Removed Tag Still Influencing Output

Viewed 40

Hi,

from bs4 import BeautifulSoup

html = '<p>This <i>is</i> a test.</p>'
soup = BeautifulSoup(html, 'lxml')

print(soup)
for tag in soup.find_all('i'):
    tag.replace_with('is')
print(soup)

print("\n")

print(soup.prettify())

print("\n")

for string in soup.stripped_strings:
    print(string)

This programs outputs the following:

<html><body><p>This <i>is</i> a test.</p></body></html>
<html><body><p>This is a test.</p></body></html>


<html>
 <body>
  <p>
   This
   is
   a test.
  </p>
 </body>
</html>


This
is
a test

Why though? Why is the string still splitted in three parts as if the removed tag was still present? If I use <p>This is a test.</p> (which is the output after I replaced the tag) as my starting html everything is working fine.

What am I doing wrong?

Thanks in Advance

1 Answers

It seems it replace <i>is</i> with is but it doesn't replace node in tree and it still treads is as separated item in tree.

You would have to convert tree to string and parse it again to get it as single node in tree.

html = str(soup)
#print(html)
soup = BeautifulSoup(html, 'lxml')

If you want text as one string then you can try get_text(strip=True, separator=" ")

from bs4 import BeautifulSoup

html = '<p>This <i>is</i> a test.</p>'
soup = BeautifulSoup(html, 'lxml')

print(soup.get_text(strip=True, separator=" "))
Related