Beautiful Soup – smooth Method


Beautiful Soup – smooth() Method



”;


Method Description

After calling a bunch of methods that modify the parse tree, you may end up with two or more NavigableString objects next to each other. The smooth() method smooths out this element”s children by consolidating consecutive strings. This makes pretty-printed output look more natural following a lot of operations that modified the tree.

Syntax


smooth()

Parameters

This method has no parameters.

Return Type

This method returns the given tag after smoothing.

Example 1


html =''''''<html>
<head>
    <title>TutorislsPoint/title>
</head>
<body>
Some Text
    <div></div>
    <p></p>
    <div>Some more text</div>
    <b></b>
    <i></i> # COMMENT
</body>
</html>''''''

from bs4 import BeautifulSoup

soup = BeautifulSoup(html, "html.parser")
soup.find(''body'').sm
for item in soup.find_all():
   if not item.get_text(strip=True):
      p = item.parent
      item.replace_with('''')
      p.smooth()

print (soup.prettify())

Output


<html>
   <head>
      <title>
         TutorislsPoint/title>
      </title>
   </head>
   <body>
      Some Text
      <div>
         Some more text
      </div>
      # COMMENT
   </body>
</html>

Example 2


from bs4 import BeautifulSoup

soup = BeautifulSoup("<p>Hello</p>", ''html.parser'')
soup.p.append(", World")

soup.smooth()
print (soup.p.contents)

print(soup.p.prettify())

Output


[''Hello, World'']
<p>
   Hello, World
</p>

Advertisements

”;

Leave a Reply

Your email address will not be published. Required fields are marked *