Given the HTML string, returns a LXML tree object. The tree is wrapped in
    <div> elements if it doesn't have a top level tag or parsing would
    otherwise result in an error. The wrapping can be later removed with
    strip_wrapping().
495108
def get_html_tree(html):
    

    parser = lxml.html.HTMLParser(encoding='utf-8')
    html = html.encode('utf8')

    try:
        tree = lxml.html.fromstring(html, parser=parser)
    except lxml.etree.Error:
        # E.g. empty document. Use dummy <div>
        tree = lxml.html.fromstring('<div></div>')

    # If the document doesn't start with a top level tag, wrap it with a <div>
    # that will be later stripped out for consistent behavior.
    if tree.tag not in lxml.html.defs.top_level_tags:
        html = b'<div>%s</div>' % html
        tree = lxml.html.fromstring(html, parser=parser)

    # HACK for Outlook emails, where tags like <o:p> are rendered as <p>. We
    # can generally ignore these tags so we replace them with <span>, which
    # doesn't cause a line break. Also, we can't look up the element path of
    # tags that contain colons. When rendering the tree, we will restore the
    # tag name.
    for el in tree.iter():
        if el.nsmap or (isinstance(el.tag, string_class) and ':' in el.tag):
            if el.nsmap:
                actual_tag_name = '{}:{}'.format(list(el.nsmap.keys())[0], el.tag)
            else:
                actual_tag_name = el.tag
            el.tag = 'span'
            el.attrib['__tag_name'] = actual_tag_name

    return tree
1093692
def parseXRDS(text):
    
    try:
        # lxml prefers to parse bytestrings, and occasionally chokes on a
        # combination of text strings and declared XML encodings -- see
        # https://github.com/necaris/python3-openid/issues/19
        # To avoid this, we ensure that the 'text' we're parsing is actually
        # a bytestring
        bytestring = text.encode('utf8') if isinstance(text, str) else text
        element = SafeElementTree.XML(bytestring)
    except (SystemExit, MemoryError, AssertionError, ImportError):
        raise
    except Exception as why:
        exc = XRDSError('Error parsing document as XML')
        exc.reason = why
        raise exc
    else:
        tree = ElementTree.ElementTree(element)
        if not isXRDS(tree):
            raise XRDSError('Not an XRDS document')

        return tree
1293782
def textFromHTML(html):
    
    cleaner = lxml.html.clean.Cleaner(scripts=True)
    cleaned = cleaner.clean_html(html)
    return lxml.html.fromstring(cleaned).text_content()
590031
def htmlReadDoc(cur, URL, encoding, options):
    
    ret = libxml2mod.htmlReadDoc(cur, URL, encoding, options)
    if ret is None:raise treeError('htmlReadDoc() failed')
    return xmlDoc(_obj=ret)
890213
def parse(html_string, wrapper=Parser, *args, **kwargs):
    
    return Parser(lxml.html.fromstring(html_string), *args, **kwargs)
1313925
def xpathEval(self, str):
        
        ret = libxml2mod.xmlXPathEval(str, self._o)
        if ret is None:raise xpathError('xmlXPathEval() failed')
        return xpathObjectRet(ret)
900632
def _xml_convert(self, element):
        

        children = list(element)

        if len(children) == 0:
            return self._type_convert(element.text)
        else:
            # if the fist child tag is list-item means all children are list-item
            if children[0].tag == "list-item":
                data = []
                for child in children:
                    data.append(self._xml_convert(child))
            else:
                data = {}
                for child in children:
                    data[child.tag] = self._xml_convert(child)

            return data
764463
def from_string(cls, jss, xml_string):
        
        root = ElementTree.fromstring(xml_string.encode('utf-8'))
        return cls(jss, root)
914855
def parseXRDS(text):
    
    try:
        element = ElementTree.XML(text)
    except XMLError, why:
        exc = XRDSError('Error parsing document as XML')
        exc.reason = why
        raise exc
    else:
        tree = ElementTree.ElementTree(element)
        if not isXRDS(tree):
            raise XRDSError('Not an XRDS document')

        return tree
1231116
def parse_navigation_html_to_tree(html, id):
    
    def xpath(x):
        return html.xpath(x, namespaces=HTML_DOCUMENT_NAMESPACES)
    try:
        value = xpath('//*[@data-type="binding"]/@data-value')[0]
        is_translucent = value == 'translucent'
    except IndexError:
        is_translucent = False
    if is_translucent:
        id = TRANSLUCENT_BINDER_ID
    tree = {'id': id,
            'title': xpath('//*[@data-type="document-title"]/text()')[0],
            'contents': [x for x in _nav_to_tree(xpath('//xhtml:nav')[0])]
            }
    return tree
1161414
def html(self) -> _BaseHTML:
        
        if self._html:
            return self.raw_html.decode(self.encoding, errors='replace')
        else:
            return etree.tostring(self.element, encoding='unicode').strip()
445887
def html(string)
      ptr = Libcgraph.agstrdup_html(graph.ptr, string)
      string = ptr.read_string
      Libcgraph.agstrfree graph.ptr, ptr

      string
    end
1037596
def _extract_body(self):
        

        def is_descendant_node(parent, node):
            node = node.getparent()
            while node is not None:
                if node == parent:
                    return True
                node = node.getparent()
            return False

        for pattern in self.config.body:
            items = self.parsed_tree.xpath(pattern)

            if len(items) == 1:
                if self.config.prune:
                    self.body = Document(etree.tostring(items[0])).summary()

                else:
                    self.body = etree.tostring(items[0])

                # We've got a body now.
                break

            else:
                appended_something = False
                body = etree.Element("root")

                for item in items:
                    if item.getparent() is None:
                        continue

                    is_descendant = False

                    for parent in body:
                        if (is_descendant_node(parent, item)):
                            is_descendant = True
                            break

                    if not is_descendant:

                        if self.config.prune:

                            # Clean with readability. Needs
                            # to-string conversion first.
                            pruned_string = Document(
                                etree.tostring(item)).summary()

                            # Re-parse the readability string
                            # output and include it in our body.
                            new_tree = etree.parse(
                                StringIO(pruned_string), self.parser)

                            failed = False

                            try:
                                body.append(
                                    new_tree.xpath('//html/body/div/div')[0]
                                )
                            except IndexError:

                                if 'id="readabilityBody"' in pruned_string:
                                    try:
                                        body.append(
                                            new_tree.xpath('//body')
                                        )
                                    except:
                                        failed = True

                                else:
                                    failed = True

                            if failed:
                                LOGGER.error(u'Pruning item failed:'
                                             u'\n\n%s\n\nWe got: “%s” '
                                             u'and skipped it.',
                                             etree.tostring(
                                                 item).replace(u'\n', u''),
                                             pruned_string.replace(u'\n', u''),
                                             extra={'siteconfig':
                                                    self.config.host})
                                pass

                        else:
                            body.append(item)

                        appended_something = True

                if appended_something:
                    self.body = etree.tostring(body)

                    # We've got a body now.
                    break
654733
def fromstring(cls, string):
        
        parser = etree.XMLParser(remove_blank_text=True)
        root = etree.fromstring(string, parser)
        tree = root.getroottree()

        return cls.fromtree(tree)
384054
private Object parseXmlElement(Element element) {
        if(element.getFirstChild() instanceof Text) {
            Text text = (Text) element.getFirstChild();
            return text.getData();
        }
        return element;
    }
730286
def get_element_tree(parent_to_parse):
    

    if isinstance(parent_to_parse, ElementTree):
        return parent_to_parse

    element = get_element(parent_to_parse)

    return ElementTree() if element is None else ElementTree(element)
131650
def strip_tags(self, content, smart=False):
        
        from lxml.html import fromstring

        return get_node_text(fromstring(content), smart=smart)