elif response.headers.subtype == 'xml' and etree is not None: try: with TrapErrors(): data = etree.ElementTree(etree.fromstring(data, parser=self.xml_parser)) data_type = 'etree' except TrapError: data_type = 'broken_xml' else: data_type = 'unknown' if response.headers.maintype == 'image' and Image is not None: try: with TrapErrors(): fileobj = stringio.StringIO(data) data = Image.open(fileobj) data.load() data_type = 'image' except TrapError: data_type = 'broken_image' return Response(code=response.code, msg=response.msg, url=url, real_url=response.url, data_type=data_type, main_type=response.headers.maintype, sub_type=response.headers.subtype, data=data, complete=complete, raw=raw) def decode_entities(html): """Convert HTML entity-encoded characters back to bytes""" for a in ((x, unichr(int(v))) for x, v in entity_dec_re.findall(html)): html = html.replace(*a) for a in ((x, unichr(htmlentitydefs.name2codepoint[name])) for x, name in entity_name_re.findall(html)): html = html.replace(*a)