

def build_html_tree(self):
    from grab.base import GLOBAL_STATE
    if (self._lxml_tree is None):
        fix_setting = self.grab.config['fix_special_entities']
        body = self.unicode_body(fix_special_entities=fix_setting).strip()
        if self.grab.config['lowercased_tree']:
            body = body.lower()
        if self.grab.config['strip_null_bytes']:
            body = body.replace(NULL_BYTE, '')
        if six.PY3:
            body = RE_UNICODE_XML_DECLARATION.sub('', body)
        else:
            body = RE_XML_DECLARATION.sub('', body)
        if (not body):
            body = '<html></html>'
        start = time.time()
        try:
            self._lxml_tree = self._build_dom(body, 'html')
        except Exception as ex:
            if (isinstance(ex, ParserError) and ('Document is empty' in str(ex)) and ('<html' not in body)):
                body = '<html>%s</html>'.format(body)
                self._lxml_tree = self._build_dom(body, 'html')
            elif (isinstance(ex, TypeError) and ("object of type 'NoneType' has no len" in str(ex)) and ('<html' not in body)):
                body = '<html>%s</html>'.format(body)
                self._lxml_tree = self._build_dom(body, 'html')
            else:
                raise
        GLOBAL_STATE['dom_build_time'] += (time.time() - self)
    return self._lxml_tree

dataset/ETHPy150Open lorien/grab/grab/document.py DomTreeExtension.build_html_tree/VarMisuse@302/435 `start`->`self`