Convert HTML to Plain Text With Beautiful Soup
Step-by-step instructions for converting HTML to plain text with Beautiful Soup, including output checks and common fixes.
Before you start
This process runs locally in Python. You need a Python environment with Beautiful Soup installed. The standard installation command is: pip install beautifulsoup4. For most HTML-to-text work, the built-in html.parser is sufficient and avoids an extra dependency. If you are working with poorly formed markup, you can optionally install a more tolerant parser such as lxml or html5lib, but this is not required for basic conversion.
Step-by-step instructions
First, import BeautifulSoup and create a soup object from your HTML string. For example: soup = BeautifulSoup(html_string, "html.parser"). If the HTML is in a file, open the file and read its contents before passing the resulting string to BeautifulSoup.
Next, produce the plain text. The simplest approach is to call: text = soup.get_text(). This removes all tags and returns only the visible text content. By default, get_text() may produce long runs without consistent spacing, depending on the markup.
To control whitespace, pass the separator parameter: soup.get_text(separator=" "). This inserts a space between text pieces. A newline separator such as soup.get_text(separator="\n") can help preserve block boundaries, especially when headings and paragraphs should remain readable.
If you want a more compact result, call: soup.get_text(separator=" ", strip=True). This removes leading and trailing whitespace from each extracted text fragment. Be aware that strip=True can join words that were originally separated by line breaks or spacing in the source.
For images and captions, remember that get_text() returns visible text only. Alt text from an image is not included because it is an attribute, not text content. If you need alt text or other attribute values, extract them separately with methods such as soup.img.get("alt") when an img tag exists.
Check the output
Compare a short section of the produced text against the original page or HTML. Ensure that visible headings, paragraphs, and list items appear without tags or script content. Check that spacing is readable and that no visible words are missing. If your source is local HTML, you can also print the first lines of the result to inspect line breaks and gaps.
The conversion is lossy by design. Font styling, link destinations, table boundaries, and the position of images are not represented in plain text. Audio or video elements do not contribute text unless the original markup contains a text fallback or caption that is visible in the text stream.
Common problems and next steps
If script or style content appears in the result, remove those elements before extracting text. You can use soup.find("script") or soup.find("style") and call decompose() on each matching element. In pages with many such elements, iterate over soup(["script", "style"]) and decompose them one by one.
If spacing looks messy, adjust the separator and strip settings. A newline separator often produces cleaner line breaks than a space separator when the HTML contains block-level elements. If the result is too empty, confirm that the HTML string was loaded correctly and that the expected text is not being generated dynamically by a script.
If you need to ensure a single space between words, split the output and rejoin it: " ".join(text.split()). This normalizes all whitespace. Use this only when you do not need to preserve paragraph boundaries.
If you plan to include link text, use get_text() for the visible label and retrieve the destination separately through the tag's href attribute. Do not attempt to recover the original formatting or layout from the plain text result.