parse – Parsing XML/HTML
This module contains everything you need to create XIST objects by parsing files, strings, URLs etc.
Parsing XML is done with a pipelined approach. The first step in the pipeline
is a source object that provides the input for the rest of the pipeline.
The next step is the XML parser. It turns the input source into an iterator over
parsing events (an “event stream”). Further steps in the pipeline might resolve
namespace prefixes (NS), and instantiate XIST classes
(Node). The final step in the pipeline is either building an
XML tree via tree() or an iterative parsing step (similar to ElementTrees
iterparse() function) via itertree().
Parsing a simple HTML string might e.g. look like this:
>>> from ll.xist import xsc, parse
>>> from ll.xist.ns import html
>>> source = b"<a href='http://www.python.org/'>Python</a>"
>>> doc = parse.tree(
... parse.String(source),
... parse.Expat(),
... parse.NS(html),
... parse.Node(pool=xsc.Pool(html)),
... )
>>> doc.string()
'<a href="http://www.python.org/">Python</a>'
A source object is an iterable object that produces the input byte string for the parser (possibly in multiple chunks) (and information about the URL of the input):
>>> from ll.xist import parse
>>> list(parse.String(b"<a href='http://www.python.org/'>Python</a>"))
[('url', URL('STRING')),
('bytes', "<a href='http://www.python.org/'>Python</a>")]
All subsequent objects in the pipeline are callable objects, get the input iterator as an argument and return an iterator over events themselves. The following code shows an example of an event stream:
>>> from ll.xist import parse
>>> source = b"<a href='http://www.python.org/'>Python</a>"
>>> list(parse.events(parse.String(source), parse.Expat()))
[('url', URL('STRING')),
('position', (0, 0)),
('enterstarttag', 'a'),
('enterattr', 'href'),
('text', 'http://www.python.org/'),
('leaveattr', 'href'),
('leavestarttag', 'a'),
('position', (0, 39)),
('text', 'Python'),
('endtag', 'a')]
An event is a tuple consisting of the event type and the event data. Different stages in the pipeline produce different event types. The following event types can be produced by source objects:
"url"The event data is the URL of the source. Usually such an event is produced only once at the start of the event stream. For sources that have no natural URL (like strings or streams) the URL can be specified when creating the source object.
"bytes"This event is produced by source objects (and
Transcoderobjects). The event data is a byte string."str"The event data is a string. This event is produced by
Decoderor source objects. Note that the only predefined pipeline objects that can handle"str"events areEncoderobjects, i.e. normally a parser handles"bytes"events, but not"str"events.
The following type of events are produced by parsers (in addition to the
"url" event from above):
"position"The event data is a tuple containing the line and column number in the source (both starting with 0). All the following events should use this position information until the next position event.
"xmldecl"The XML declaration. The event data is a dictionary containing the keys
"version","encoding"and"standalone". Parsers may omit this event."begindoctype"The begin of the doctype. The event data is a dictionary containing the keys
"name","publicid"and"systemid". Parsers may omit this event."enddoctype"The end of the doctype. The event data is
None. (If there is no internal subset, the"enddoctype"event immediately follows the"begindoctype"event). Parsers may omit this event."comment"A comment. The event data is the content of the comment.
"text"Text data. The event data is the text content. Parsers should try to avoid outputting multiple text events in sequence.
"cdata"A CDATA section. The event data is the content of the CDATA section. Parsers may report CDATA sections as
"text"events instead of"cdata"events."enterstarttag"The beginning of an element start tag. The event data is the element name.
"leavestarttag"The end of an element start tag. The event data is the element name. The parser will output events for the attributes between the
"enterstarttag"and the"leavestarttag"event."enterattr"The beginning of an attribute. The event data is the attribute name.
"leaveattr"The end of an attribute. The event data is the attribute name. The parser will output events for the attribute value between the
"enterattr"and the"leaveattr"event. (In almost all cases this is one text event)."endtag"An element end tag. The event data is the element name.
"procinst"A processing instruction. The event data is a tuple consisting of the processing instruction target and the data.
"entity"An entity reference. The event data is the entity name.
The following events are produced for elements and attributes in namespace mode
(instead of those without the ns suffix). They are produced by NS
objects or by Expat objects when the ns argument is true (i.e.
the expat parser performs the namespace resolution):
"enterstarttagns"The beginning of an element start tag in namespace mode. The event data is an (namespace name, element name) tuple.
"leavestarttagns"The end of an element start tag in namespace mode. The event data is an (namespace name, element name) tuple.
"enterattrns"The beginning of an attribute in namespace mode. The event data is an (namespace name, element name) tuple.
"leaveattrns"The end of an attribute in namespace mode. The event data is an (namespace name, element name) tuple.
"endtagns"An element end tag in namespace mode. The event data is an (namespace name, element name) tuple.
Once XIST nodes have been instantiated (by Node objects) the
following events are used:
"xmldeclnode"The XML declaration. The event data is an instance of
ll.xist.ns.xml.XML."doctypenode"The doctype. The event data is an instance of
ll.xist.xsc.DocType."commentnode"A comment. The event data is an instance of
ll.xist.xsc.Comment."textnode"Text data. The event data is an instance of
ll.xist.xsc.Text."enterelementnode"The beginning of an element. The event data is an instance of
ll.xist.xsc.Element(or one of its subclasses). The attributes of the element object are set, but the element has no content yet."leaveelementnode"The end of an element. The event data is an instance of
ll.xist.xsc.Element."procinstnode"A processing instruction. The event data is an instance of
ll.xist.xsc.ProcInst."entitynode"An entity reference. The event data is an instance of
ll.xist.xsc.Entity.
For consuming event streams there are three functions:
events()This generator simply outputs the events.
tree()This function builds an XML tree from the events and returns it.
itertree()This generator builds a tree like
tree(), but returns events during certain steps in the parsing process.
Example
The following example shows a custom generator in the pipeline that lowercases all element and attribute names:
from ll.xist import xsc, parse
from ll.xist.ns import html
def lowertag(input):
for (event, data) in input:
if event in {"enterstarttag", "leavestarttag", "endtag", "enterattr", "leaveattr"}:
data = data.lower()
yield (event, data)
e = parse.tree(
parse.String(b"<A HREF='gurk'><B>gurk</B></A>"),
parse.Expat(),
lowertag,
parse.NS(html),
parse.Node(pool=xsc.Pool(html))
)
print(e.string())
This scripts outputs:
<a href="gurk"><b>gurk</b></a>
- exception ll.xist.parse.UnknownEventError[source]
Bases:
TypeErrorThis exception is raised when a pipeline object doesn’t know how to handle an event.
- class ll.xist.parse.String[source]
Bases:
objectProvides parser input from a string.
- __init__(data, url=None)[source]
Create a
Stringobject.datamust be abytesorstrobject.urlspecifies the URL for the source (defaulting to"STRING").
- __iter__()[source]
Produces an event stream of one
"url"event and one"bytes"or"str"event for the data.
- class ll.xist.parse.Iter[source]
Bases:
objectProvides parser input from an iterator over strings.
- __init__(iterable, url=None)[source]
Create a
Iterobject.iterablemust be an iterable object producingbytesorstrobjects.urlspecifies the URL for the source (defaulting to"ITER").
- __iter__()[source]
Produces an event stream of one
"url"event followed by the"bytes"/"str"events for the data from the iterable.
- class ll.xist.parse.Stream[source]
Bases:
objectProvides parser input from a stream (i.e. an object that provides a
read()method).- __init__(stream, url=None, bufsize=8192)[source]
Create a
Streamobject.streammust have aread()method (with asizeargument).urlspecifies the URL for the source (defaulting to"STREAM").bufsizespecifies the chunksize for reads from the stream.
- __iter__()[source]
Produces an event stream of one
"url"event followed by the"bytes"/"str"events for the data from the stream.
- class ll.xist.parse.File[source]
Bases:
objectProvides parser input from a file.
- __init__(filename, bufsize=8192)[source]
Create a
Fileobject.filenameis the name of the file and may start with~or~userfor the home directory of the current or the specified user.bufsizespecifies the chunksize for reads from the file.
- __iter__()[source]
Produces an event stream of one
"url"event followed by the"bytes"events for the data from the file.
- class ll.xist.parse.URL[source]
Bases:
objectProvides parser input from a URL.
- __init__(name, bufsize=8192, *args, **kwargs)[source]
Create a
URLobject.nameis the URL.bufsizespecifies the chunksize for reads from the URL.argsandkwargswill be passed on to theopen()method of the URL object.The URL for the input will be the final URL for the resource (i.e. it will include redirects).
- __iter__()[source]
Produces an event stream of one
"url"event followed by the"bytes"events for the data from the URL.
- class ll.xist.parse.ETree[source]
Bases:
objectProduces a (namespaced) event stream from an object that supports the ElementTree API.
- __init__(data, url=None, defaultxmlns=None)[source]
Create an
ETreeobject. Arguments have the following meaning:dataAn object that supports the ElementTree API.
urlThe URL of the source. Defaults to
"ETREE".defaultxmlnsThe namespace name (or a namespace module containing a namespace name) that will be used for all elements that don’t have a namespace.
- __iter__()[source]
Produces an event stream of namespaced parsing events for the ElementTree object passed as
datato the constructor.
- class ll.xist.parse.Decoder[source]
Bases:
objectDecode the
bytesobject produced by the previous object in the pipeline tostrobject.This input object can be a source object or any other pipeline object that produces
bytesobjects.
- class ll.xist.parse.Encoder[source]
Bases:
objectEncode the
strobjects produced by the previous object in the pipeline tobytesobjects.This input object must be a pipeline object that produces string output (e.g. a
Decoderobject).This can e.g. be used to parse a
strobject instead of abytesobject like this:>>> from ll.xist import xsc, parse >>> from ll.xist.ns import html >>> source = "<a href='http://www.python.org/'>Python</a>" >>> doc = parse.tree( ... parse.String(source), ... parse.Encoder(encoding="utf-8"), ... parse.Expat(encoding="utf-8"), ... parse.NS(html), ... parse.Node(pool=xsc.Pool(html)), ... ) >>> doc.string() '<a href="http://www.python.org/">Python</a>'
- class ll.xist.parse.Transcoder[source]
Bases:
objectTranscode the
bytesobject of the input object into another encoding.This input object can be a source object or any other pipeline object that produces
bytesevents.- __init__(fromencoding=None, toencoding=None)[source]
Create a
Transcoderobject.fromencodingis the encoding of the input.toencodingis the encoding of the output. If any of them isNonethe encoding will be detected from the data.
- class ll.xist.parse.Expat[source]
Bases:
ParserA parser using Pythons builtin
expatparser.- __init__(encoding=None, xmldecl=False, doctype=False, loc=True, cdata=False, ns=False)[source]
Create an
Expatparser. Arguments have the following meaning:encodingstring orNoneForces the parser to use the specified encoding. The default
Noneresults in the encoding being detected from the XML itself.xmldeclboolShould the parser produce events for the XML declaration?
doctypeboolShould the parser produce events for the document type?
locboolShould the parser produce
"location"events?cdataboolShould the parser output CDATA sections as
"cdata"events? (Ifcdatais false"text"events are output instead.)nsboolIf
nsis true, the parser performs namespace processing itself, i.e. it will emit"enterstarttagns","leavestarttagns","endtagns","enterattrns"and"leaveattrns"events instead of"enterstarttag","leavestarttag","endtag","enterattr"and"leaveattr"events.
- __call__(input)[source]
Return an iterator over the events produced by
input.
- class ll.xist.parse.SGMLOP[source]
Bases:
ParserA parser based on
sgmlop.- __init__(encoding=None, cdata=False)[source]
Create a
SGMLOPparser. Arguments have the following meaning:encodingstring orNoneForces the parser to use the specified encoding. The default
Noneresults in the encoding being detected from the XML itself.cdataboolShould the parser output CDATA sections as
"cdata"events? (Ifcdatais false output"text"events instead.)
- __call__(input)[source]
Return an iterator over the events produced by
input.
- class ll.xist.parse.NS[source]
Bases:
objectAn
NSobject is used in a parsing pipeline to add support for XML namespaces. It replaces the"enterstarttag","leavestarttag","endtag","enterattr"and"leaveattr"events with the appropriate namespace version of the events (i.e."enterstarttagns"etc.) where the event data is a(namespace, name)tuple.The output of an
NSobject in the stream looks like this:>>> from ll.xist import parse >>> from ll.xist.ns import html >>> list(parse.events( ... parse.String(b"<a href='http://www.python.org/'>Python</a>"), ... parse.Expat(), ... parse.NS(html) ... )) [('url', URL('STRING')), ('position', (0, 0)), ('enterstarttagns', ('http://www.w3.org/1999/xhtml', 'a')), ('enterattrns', (None, 'href')), ('text', 'http://www.python.org/'), ('leaveattrns', (None, 'href')), ('leavestarttagns', ('http://www.w3.org/1999/xhtml', 'a')), ('position', (0, 39)), ('text', 'Python'), ('endtagns', ('http://www.w3.org/1999/xhtml', 'a'))]
- __init__(prefixes=None, **kwargs)[source]
Create an
NSobject.prefixes(if notNone) can be a namespace name (or module), which will be used for the empty prefix, or a dictionary that maps prefixes to namespace names (or modules).kwargsmaps prefixes to namespaces names too. If a prefix is in bothprefixesandkwargs,kwargswins.
- class ll.xist.parse.Node[source]
Bases:
objectA
Nodeobject is used in a parsing pipeline to instantiate XIST nodes. It consumes a namespaced event stream:>>> from ll.xist import xsc, parse >>> from ll.xist.ns import html >>> list(parse.events( ... parse.String(b"<a href='http://www.python.org/'>Python</a>"), ... parse.Expat(), ... parse.NS(html), ... parse.Node(pool=xsc.Pool(html)) ... )) [('enterelementnode', <element ll.xist.ns.html.a xmlns='http://www.w3.org/1999/xhtml' (no children/1 attr) location='STRING:0:0' at 0x10a683550>), ('textnode', <ll.xist.xsc.Text content='Python' location='STRING:0:39' at 0x10a5e1170>), ('leaveelementnode', <element ll.xist.ns.html.a xmlns='http://www.w3.org/1999/xhtml' (no children/1 attr) location='STRING:0:0' at 0x10a683550>) ]
The event data of all events are XIST nodes. The element node from the
"enterelementnode"event already has all attributes set. There will be no events for attributes.- __init__(pool=None, base=None, loc=True)[source]
Create a
Nodeobject.poolmay beNoneor axsc.Poolobject and specifies which classes used for creating element, entity and processsing instruction instances.basespecifies the base URL for interpreting relative links in the input.locspecified whether location information should be attached to the nodes that get generated (thestartlocattribute (andendlocattribute for elements))
- class ll.xist.parse.Tidy[source]
Bases:
objectA
Tidyobject parses (potentially ill-formed) HTML from a source into a (non-namespaced) event stream by using lxml’s HTML parser:>>> from ll.xist import parse >>> list(parse.events(parse.URL("http://www.yahoo.com/"), parse.Tidy())) [('url', URL('http://de.yahoo.com/?p=us')), ('enterstarttag', 'html'), ('enterattr', 'class'), ('text', 'y-fp-bg y-fp-pg-grad bkt708'), ('leaveattr', 'class'), ('enterattr', 'lang'), ('text', 'de-DE'), ('leaveattr', 'lang'), ('enterattr', 'style'), ('leaveattr', 'style'), ('leavestarttag', 'html'), ...
- __init__(encoding=None, xmldecl=False, doctype=False)[source]
Create a new
Tidyobject. Parameters have the following meaning:encodingstring orNoneThe encoding of the input. If
encodingisNoneit will be automatically detected by the HTML parser.xmldeclboolShould the parser produce events for the XML declaration?
doctypeboolShould the parser produce events for the document type?
- ll.xist.parse.events(*pipeline)[source]
Return an iterator over the events produced by the pipeline objects in
pipeline.
- ll.xist.parse.tree(*pipeline, validate=False)[source]
Return a tree of XIST nodes from the event stream
pipeline.pipelinemust output only events that contain XIST nodes, i.e. the event types"xmldeclnode","doctypenode","commentnode","textnode","enterelementnode","leaveelementnode","procinstnode"and"entitynode".If
validateis true, the tree is validated, i.e. it is checked if the structure of the tree is valid (according to themodelattribute of each element node), if no undeclared elements or attributes have been encountered, all required attributes are specified and all attributes have allowed values.The node returned from
tree()will always be aFragobject.Example:
>>> from ll.xist import xsc, parse >>> from ll.xist.ns import xml, html, chars >>> doc = parse.tree( ... parse.URL("http://www.python.org/"), ... parse.Tidy(), ... parse.NS(html), ... parse.Node(pool=xsc.Pool(xml, html, chars)) ... ) >>> doc[0] <element ll.xist.ns.html.html xmlns='http://www.w3.org/1999/xhtml' (7 children/3 attrs) location='https://www.python.org/:?:?' at 0x110a4ecd0>
- ll.xist.parse.itertree(*pipeline, entercontent=True, enterattrs=False, enterattr=False, enterelementnode=False, leaveelementnode=True, enterattrnode=True, leaveattrnode=False, selector=None, validate=False)[source]
Parse the event stream
pipelineiteratively.itertree()still builds a tree, but it returns an iterator ofxsc.Cursorobjects that tracks changes to the tree as it is built.validatespecifies whether each node should be validated after it has been fully parsed.The rest of the arguments can be used to control when
itertree()returns to the calling code. For an explanation of their meaning see the classll.xist.xsc.Cursor.Example:
>>> from ll.xist import xsc, parse >>> from ll.xist.ns import xml, html, chars >>> for c in parse.itertree( ... parse.URL("http://www.python.org/"), ... parse.Tidy(), ... parse.NS(html), ... parse.Node(pool=xsc.Pool(xml, html, chars)), ... selector=html.a/html.img ... ): ... print(c.path[-1].attrs.src, "-->", c.path[-2].attrs.href) https://www.python.org/static/img/python-logo.png --> https://www.python.org/