'module' object has no attribute 'unescape'

Viewed 12558

This code works perfectly on the console but when I implement it in my flask application it says that there's an AttributeError

        clean = html.unescape(tweet.text)

        final = re.sub(r'\w+:\/{2}[\d\w-]+(\.[\d\w-]+)*(?:(?:\/[^\s/]*))*', '', clean)

I also tried import html inside the for loop

and from html import unescape

4 Answers

Please note that HTMLParser.unescape was removed from Python since version 3.9.0a1.

Update (due to popular demand)

Beginning with Python 3.4 you can use html.unescape, see https://docs.python.org/3/library/html.html

Summing up all the answers. If you use python of version > 3.9 HTMLParser doesn't work. From py 3.4 html does work. use:

import html

If you use python <3.4 (including 2.X) html doesn't work:

import HTMLParser
html = HTMLParser.HTMLParser()

If you want compatibility to both:

import sys
if sys.version_info[0] > 3:
  import html

else:
  import HTMLParser
  html = HTMLParser.HTMLParser()

html.unescape(my_string)
Related