+ try:
+ html_tree = fromstring(html_text)
+ except ValueError as e:
+ if e.args[0].startswith(
+ 'Unicode strings with encoding declaration are not supported.'
+ ' Please use bytes input'
+ ):
+ if not charset:
+ match = re.search(
+ '<\\?xml version="(\\d|.)+" encoding="([^"]+)"\\?>',
+ html_text, re.U)
+ if match:
+ charset = match.group(2)
+ if charset:
+ html_text = html_text.encode(charset)
+ html_tree = fromstring(html_text)
+ else:
+ return None
+ else:
+ raise