[lxml-dev] how knowing the types return by .xpath
Sergio Monteiro Basto
sergio at sergiomb.no-ip.org
Wed Feb 4 21:25:19 CET 2009
On Wed, 2009-02-04 at 20:56 +0100, Stefan Behnel wrote:
> Hi,
>
> Sergio Monteiro Basto wrote:
> > Example:
> >
> > from lxml import etree
> >
> > f = open(options.file).read()
> >
> > hparser = etree.HTMLParser(encoding='utf-8', remove_comments=True)
> > etree_document = etree.HTML(f, parser=hparser)
> >
> > elems=etree_document.xpath(strxpath)
> >
> > for frags in elems:
> > print type (frags)
> > (...)
> >
> > if strxpath is equal //h1/following-sibling::text()
> > prints
> > <type 'lxml.etree._ElementUnicodeResult'>
> >
> > if strxpath is equal //div[@class="news"]
> > prints
> > <type 'lxml.etree._Element'>
> >
> > How I do a "if" to detected the types ?
>
> It's actually rare that the expected type isn't known in advance, but for
> this kind of use case, you can just test the type as usual, i.e. use
> isinstance() to check for basestring, float or list.
elems (in the example) is a list and each element of the list (elems)
could be a lxml.etree._ElementUnicodeResult or a lxml.etree._Element'
yes ,
isinstance(lxml.etree._ElementUnicodeResult, basestring) is true
and
isinstance(xml.etree._Element, basestring) is false
which resolve my initial problem
but what instance is xml.etree._Element ?
>
> Stefan
Many thanks,
--
Sérgio M. B.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/x-pkcs7-signature
Size: 2192 bytes
Desc: not available
Url : http://codespeak.net/pipermail/lxml-dev/attachments/20090204/4a68f5e7/attachment.bin
More information about the lxml-dev
mailing list