bs4.FeatureNotFound: Couldn't find a tree builder with the features you requested: lxml
Fixes BeautifulSoup raising FeatureNotFound when you ask for the lxml tree builder and lxml is not installed. Use when the traceback names the lxml feature set. Not for `No module named 'bs4'` or for ImportError on the BeautifulSoup name itself.
BeautifulSoup does not ship its own HTML parser. When you write BeautifulSoup(html, 'lxml') and lxml is not installed, you get FeatureNotFound instead of a soup. Install it with pip install lxml and the same line works. If you cannot install lxml, switch the second argument to 'html.parser' (built into Python, no install needed) and move on.
The error
bs4.FeatureNotFound: Couldn't find a tree builder with the features you requested: lxmlFix it
- Install lxml
pip install lxmlExpected: pip reports Successfully installed lxml-....
- Re-run your parse line unchanged
python -c "from bs4 import BeautifulSoup; print(BeautifulSoup('hi', 'lxml').get_text())"Expected: it prints hi. The line that raised now works because the requested tree builder exists.
- Alternative when you cannot install anything
Change the parser argument to the built-in one:
soup = BeautifulSoup(html, 'html.parser')Expected: parsing works with no new installs. Slightly slower and more lenient than lxml, fine for most scraping.
When this applies
- the traceback is
bs4.FeatureNotFoundand nameslxml - you passed
'lxml','lxml-xml', or'xml'as the parser argument - a library you use (pandas read_html, for example) asks for lxml under the hood
When it does NOT apply
ModuleNotFoundError: No module named 'bs4'is a different problem (install beautifulsoup4)- FeatureNotFound naming
html5libmeans install html5lib instead - an ImportError on the BeautifulSoup name is the stub/shadowing problem
Compatibility
All beautifulsoup4 4.x versions. lxml wheels exist for CPython 3.8+ on Windows, macOS, and Linux; on odd platforms you may need the libxml2 system libraries to build it.
Variant phrasings
Couldn't find a tree builder with the features you requested: html5lib
Same error, different parser. Fix is pip install html5lib, or switch to 'html.parser'.
Couldn't find a tree builder with the features you requested: lxml-xml
You asked for the XML tree builder. Same fix: pip install lxml.
Why it happens
BeautifulSoup is a tree API, not a parser. The second argument to the constructor picks which installed parsing library does the actual work. If that library is not installed, BeautifulSoup cannot build the tree and raises FeatureNotFound. The message names exactly the feature set it looked for, so the fix is installing the named parser or picking one you have.
Edge cases
- In a venv, make sure you pip-install into the same environment that runs the script.
- On Alpine/musl Linux there is no lxml wheel, so pip builds from source and needs libxml2-dev and libxslt-dev system packages.
- If lxml is installed but the error persists, you have two Pythons and installed into the wrong one.
python -m pip install lxmltargets the interpreter you run.
Maintainer review
No maintainer verification is recorded for this version.
This records the version a maintainer checked. It does not assert that the version is the latest upstream release.