1

I want to use xml.etree.ElementTree to parse an XHTML document in Python 3. The document contains   entities, so I cannot use the default parser settings. I'd like to do something similar to:

with urllib.request.urlopen(BASE_URL) as url:
        body = url.read()
        parser = ET.XMLParser()
        parser.parser.UseForeignDTD(True)
        parser.entity.update(entitydefs)
        etree = ET.ElementTree()
        root = etree.fromstring(body)

But fromstring is a free function in ElementTree. How can I achieve something similar with ElementTree instance?

brabec
  • 4,632
  • 8
  • 37
  • 63

2 Answers2

2

Well I encountered same problem. The sample code in the question and the chosen answer might work before, but right now it won't work in my Python 3.3 and Python 3.4 environment.

I finally got it working. Quoted from this Q&A.

Inspired by this post, we can just prepend some XML definition to the incoming raw HTML content, and then ElementTree would work out of box.

This works for both Python 2.6, 2.7, 3.3, 3.4.

import xml.etree.ElementTree as ET

html = '''<html>
    <div>Some reasonably well-formed HTML content.</div>
    <form action="login">
    <input name="foo" value="bar"/>
    <input name="username"/><input name="password"/>

    <div>It is not unusual to see &nbsp; in an HTML page.</div>

    </form></html>'''

magic = '''<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
            "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" [
            <!ENTITY nbsp ' '>
            ]>'''  # You can define more entities here, if needed

et = ET.fromstring(magic + html)
Community
  • 1
  • 1
RayLuo
  • 17,257
  • 6
  • 88
  • 73
1

Feed the parser:

with urllib.request.urlopen(BASE_URL) as url:
    body = url.read()
    parser = ET.XMLParser()
    parser.parser.UseForeignDTD(True)
    parser.entity.update(entitydefs)
    parser.feed(body)
    root = parser.close()   # this returns you the tree
Pavel Anossov
  • 60,842
  • 14
  • 151
  • 124
  • 1
    Original poster asked for Python 3 solution, but `parser.parser.UseForeignDTD(True)` doesn't work in Python 3. How come this answer was chosen as the correct answer? – RayLuo Feb 02 '16 at 11:50