Skip to content

Turn HTML into plain text in Python

When you need readable content rather than markup, pick the element that contains the information and extract only its text.

OUTPUT FORMATSHTMLJSONMarkdown

A parsing library is a direct fit for static HTML; a browser-automation tool is another route when you are already working with a live element.

Extract text with BeautifulSoup

Start with the parsed document, narrow it to the relevant container, and then request its text. In the supplied worked example, the body element is selected first, so headings and detail values inside that container are returned together. The resulting plain-text output keeps the product heading, the stock label, and the stock value on separate lines.

How the example is structured

The example is useful as a checklist: create the parser from the HTML, find the element that bounds the content you need, call the text method on that element, and inspect the printed result. Selecting a narrower element than the whole document helps avoid collecting unrelated text.

Use Selenium when you already have an element

If your workflow already uses Selenium, you do not need a separate parsing step for an element you have located in the browser. Read its .text property to obtain the text contained in that HTMLElement. Choose BeautifulSoup when you are parsing HTML directly, and choose the Selenium property when the element is already available through browser automation.

Which approach fits your workflow?

Parsing libraries and browser automation solve the same extraction task from different starting points. A parser works from HTML markup, while Selenium works from a live element reference. In both cases, the key decision is scope: extract from the smallest element that contains the needed content.

Frequently asked questions

What is the simplest way to extract text from an HTML page in Python?

Use a Python HTML parsing library and call its text-extraction method on the element you want. The worked example uses BeautifulSoup's get_text() after selecting the body element.

Can Selenium extract text from an element?

Yes. Selenium provides the .text property on an HTMLElement for extracting text from that element, as an alternative to a parsing-library approach.