# Parsing HTML with the XML module

**URL:** <https://discuss.python.org/t/parsing-html-with-the-xml-module/12013>\
**Category:** Python Help\
**Created:** [November 16, 2021, 7:25pm UTC](https://discuss.python.org/t/parsing-html-with-the-xml-module/12013 "2021-11-16T19:25:23Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jonathan-Sachs-NVIDIA](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/jonathan-sachs-nvidia/32/5494_2.png) [@Jonathan-Sachs-NVIDIA](https://discuss.python.org/u/Jonathan-Sachs-NVIDIA)\
**Post date:** [November 16, 2021, 7:25pm UTC](https://discuss.python.org/t/parsing-html-with-the-xml-module/12013/1 "2021-11-16T19:25:23Z")

</div>

I need to parse HTML files with the Python 3.8 `xml` package. This must be possible because some of the `xml.etree.ElementTree` methods have parameters that take `"xml"` or `"html"` as a value, but I can’t find an example of how it’s done.

I get an exception when I try to parse the HTML file:

```auto
htmlRoot = etree.ElementTree.parse(filepathname).getroot()

```

The parser throws an “undefined entity” exception when it encounters an HTML entity. I assume this is because HTML entities are predefined, while XML entities are not.

As the statement shows, I’m using the default parser. Maybe there’s an HTML parser but I haven’t found one. I’m not even sure there _are_ other parsers; maybe I’m expected to roll my own.

I don’t want to use Python’s `html` package because I need to walk a complete parsed tree like `xml.etree` provides. The html package doesn’t work that way.

I’ve found examples of parsing HTML with the `lxml` package, but `lxml` isn’t part of the standard Python configuration. That’s would be a problem for coworkers who don’t know Python and need a “plug and play” application.

---

<div class="post-metadata">

**Author:** ![MartinPacker](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/martinpacker/32/5471_2.png) [@MartinPacker](https://discuss.python.org/u/MartinPacker)\
**Post date:** [November 17, 2021, 8:21am UTC](https://discuss.python.org/t/parsing-html-with-the-xml-module/12013/2 "2021-11-17T08:21:13Z")

</div>

Note:

1. HTML isn’t necessarily XML.
2. There could be a namespace issue.
3. What’s wrong with Beautiful Soup?

---

<div class="post-metadata">

**Author:** ![tjol](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/tjol/32/3414_2.png) [@tjol](https://discuss.python.org/u/tjol)\
**Post date:** [November 17, 2021, 12:21pm UTC](https://discuss.python.org/t/parsing-html-with-the-xml-module/12013/3 "2021-11-17T12:21:06Z")

</div>

> [@Jonathan-Sachs-NVIDIA](#):
>
> This must be possible because some of the `xml.etree.ElementTree` methods have parameters that take `"xml"` or `"html"` as a value

The only functions in `xml.etree.ElementTree` that take a `method="html"` argument are functions for _writing_ markup, not for _reading_ markup.

It might be possible to feed HTML into ElementTree by providing a custom clone or subclass of [xml.etree.ElementTree.XMLParser](https://docs.python.org/3/library/xml.etree.elementtree.html#xml.etree.ElementTree.XMLParser), but that really doesn’t sound like something most people would want to do, especially as BeautifulSoup exists.

---

<div class="post-metadata">

**Author:** ![Jonathan-Sachs-NVIDIA](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/jonathan-sachs-nvidia/32/5494_2.png) [@Jonathan-Sachs-NVIDIA](https://discuss.python.org/u/Jonathan-Sachs-NVIDIA)\
**Post date:** [November 17, 2021, 2:06pm UTC](https://discuss.python.org/t/parsing-html-with-the-xml-module/12013/4 "2021-11-17T14:06:47Z")

</div>

So what would you do in a case like this? As far as I know, Python’s html module cannot be used to build an element tree, so I would have to reinvent the wheel (the element tree) to make it do what I want.

Part of my original problem statement seems to have been overlooked, so I’ll repeat it: “…`lxml` isn’t part of the standard Python configuration. That would be a problem for coworkers who don’t know Python and need a “plug and play” application.” The same would be true of Beautiful Soup.

As a side note, Beautiful Soup would give me no advantage over lxml, because the HTML I’m processing is program-generated, and will never have the kind of faulty syntax that Beautiful Soup is designed to salvage.

---

<div class="post-metadata">

**Author:** ![tjol](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/tjol/32/3414_2.png) [@tjol](https://discuss.python.org/u/tjol)\
**Post date:** [November 17, 2021, 3:26pm UTC](https://discuss.python.org/t/parsing-html-with-the-xml-module/12013/5 "2021-11-17T15:26:22Z")

</div>

Python comes with ‘batteries included’, but there will always be many tasks that are cumbersome using just the standard library. This appears to be one of them.

I’d just use a third-party module. Such as lxml, or whatever. And tell your users how to install the dependencies with `pip`. Or you could bundle your dependencies. Or provide an installer that installs everything.

Or you can just use the standard `html.parser` module. I imagine it’s easier to build your own DOM-like tree with the help of `html.parser` than repurpose `ElementTree`, but I’m really just speculating here.

---

<div class="post-metadata">

**Author:** ![fdrake](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/fdrake/32/19_2.png) [@fdrake](https://discuss.python.org/u/fdrake)\
**Post date:** [November 17, 2021, 3:57pm UTC](https://discuss.python.org/t/parsing-html-with-the-xml-module/12013/6 "2021-11-17T15:57:40Z")

</div>

You could even use `html.parser` to build a DOM using the `xml.dom.minidom` implementation. Should be fairly straightforward.

-Fred
