Last modified: Aug 31, 2026
BeautifulSoup Beautify: Pretty Print HTML
Scraping HTML often returns a messy, single-line string. It is hard to read and debug. The BeautifulSoup library offers a simple solution. The beautify() method formats the parse tree with proper indentation. This makes your output clean and readable. Let's explore how it works.
This method is essential for developers. It helps you understand the structure of a page. You can spot missing tags or nesting errors quickly. In this guide, we will cover the syntax, usage, and practical examples. You will see how beautify() transforms raw HTML into organized code.
What is BeautifulSoup beautify()?
The beautify() method is a built-in function in BeautifulSoup. It takes the parsed HTML tree and adds line breaks and spaces. The result is a well-formatted string. It does not change the original data. It only changes the presentation. This is very useful for testing and learning.
Imagine you have a complex webpage. Without formatting, it looks like a wall of text. With beautify(), each tag is on its own line. Attributes are visible. The hierarchy becomes obvious. This helps you write better selectors for your scraping tasks.
For more context on how BeautifulSoup parses documents, check our guide on What is BeautifulSoup? It covers the basics of the parser.
How to Use beautify()
Using beautify() is very straightforward. First, you create a BeautifulSoup object. Then, you call the method on that object. Finally, you print the result. Let's look at a basic example.
Here is a simple Python script. We will parse a small snippet of HTML and format it.
# Import the library
from bs4 import BeautifulSoup
# Messy HTML string
html_doc = "Test Hello World!
Second Paragraph
"
# Parse the HTML
soup = BeautifulSoup(html_doc, 'html.parser')
# Use beautify to format it
pretty_html = soup.beautify()
# Print the result
print(pretty_html)
Now, let's see the output. Notice the indentation and line breaks.
Test
Hello World!
Second Paragraph
As you can see, the output is much easier to read. The nesting is clear. This is the core functionality of beautify().
Why Use beautify()?
There are many reasons to use this method. First, it aids in debugging. When your scraper fails, a formatted view helps you find the issue. You can see if a tag is missing or if data is in the wrong place.
Second, it helps with data extraction. When you understand the structure, you can write better CSS selectors. You can target specific elements with precision. This saves time and reduces errors in your code.
Third, it's great for learning. If you are new to web scraping, seeing the tree structure is valuable. It teaches you how HTML is organized. This builds a strong foundation for more advanced techniques.
When working with complex sites, you might need to filter elements. The beautify() method works well with attrs BeautifulSoup to target specific nodes.
Advanced Example with Nested Tags
Let's try a more complex example. We'll include attributes and nested lists. This shows how beautify() handles deeper structures.
# Complex HTML with attributes
complex_html = """
- First
- Second
Link
"""
# Parse the HTML
soup = BeautifulSoup(complex_html, 'html.parser')
# Format it
pretty = soup.beautify()
# Print the result
print(pretty)
Here is the formatted output. Notice how attributes are preserved. The indentation shows the parent-child relationships clearly.
-
First
-
Second
Link
This makes it easy to see the structure. You can quickly identify the <ul> and its <li> children. This is perfect for extracting data from lists.
Common Pitfalls and Tips
One common mistake is expecting beautify() to fix broken HTML. It does not. It only formats what the parser has built. If the HTML is severely malformed, the output might still look odd. Always validate your source.
Another tip: beautify() is not for minifying code. It adds whitespace. This increases the size of the string. For production scraping, you might want to use the original string to save memory.
Also, remember that beautify() works on the Tag object. It does not work on simple strings. You must have a BeautifulSoup object to use it. If you have a Tag, you can call the method on it directly.
If you are dealing with dynamic content, you might need to handle JavaScript first. Check our article on Enable JavaScript & Cookies to see how to get the full HTML.
Alternatives to beautify()
While beautify() is great, there are other ways to format HTML. You can use the prettify() method from the html.parser module. However, beautify() is more integrated with BeautifulSoup.
You could also use external libraries like html5lib for parsing. But for simple formatting, beautify() is often enough. It's built-in and requires no extra dependencies.
For large-scale projects, you might compare it with other tools. Our guide on BeautifulSoup vs Scrapy can help you decide which is better for your needs.
Conclusion
The beautify() method is a powerful tool for any web scraper. It transforms messy HTML into a clean, readable format. This helps in debugging, learning, and writing better extraction logic.
We've seen how to use it with simple and complex examples. The method is easy to use and requires no special setup. Just parse your HTML and call beautify().
Remember, it only changes the presentation, not the data. Use it to understand your HTML structure. Then, apply your scraping logic with confidence. Happy coding!