Cleaning HTMLs to extract text I have written the following routine to clean an html raw code and extract the text.

It works, but the code is not very well written. Any ideas how to solve this faster and with fewer lines of code?

Thanks a lot for your help!

import re
def cleanhtml(raw_html):
    cleanr = re.compile('&lt;.*?&gt;')
    cleantext1 = re.sub(cleanr, ' ', raw_html)
    cleanr = re.compile('&amp;amp;')
    cleantext2 = re.sub(cleanr, '&amp;', cleantext1 )
    cleanr = re.compile('\n')
    cleantext3 = re.sub(cleanr, ' ', cleantext2)
    cleanr = re.compile('&amp;nbsp;')
    cleantext4 = re.sub(cleanr, ' ', cleantext3)
    cleanr = re.compile('&amp;#8217;')
    cleantext5 = re.sub(cleanr, '\'', cleantext4)
    cleanr = re.compile('&amp;#8230;')
    cleantext6 = re.sub(cleanr, '...', cleantext5)
    cleanr = re.compile('&amp;#8216;')
    cleantext7 = re.sub(cleanr, '\'', cleantext6)
    cleanr = re.compile('&amp;#8221;')
    cleantext8 = re.sub(cleanr, '\'', cleantext7)
    cleanr = re.compile('&amp;#8220;')
    cleantext9 = re.sub(cleanr, '\'', cleantext8)
    cleanr = re.compile('&amp;#8211;')
    cleantext10 = re.sub(cleanr, '\'', cleantext9)
    cleanr = re.compile('&amp;#8212;')
    cleantext11 = re.sub(cleanr, '\'', cleantext10)
    cleanr = re.compile('&amp;#38;')
    cleantext12 = re.sub(cleanr, '&amp;', cleantext11)
    cleanr = re.compile('&amp;#2026;')
    cleantext13 = re.sub(cleanr, '...', cleantext12)
    cleanr = re.compile('&amp;#160;')
    cleantext14 = re.sub(cleanr, ' ', cleantext13)
    cleanr = re.compile('&amp;#038;')
    cleantext15 = re.sub(cleanr, '&amp;', cleantext14)
    return cleantext15.strip()
Since you have basically the same statements over and over with different parameters, this can be turned into a loop. You can pair the pattern and the string you want to use to replace in a dict. This makes it clear what is paired with what. Take a look:

import re

def strip_html(raw_html: str) -&gt; str:
    pairs = {
        '&lt;.*?&gt;': ' ',
        '&amp;amp;': '&amp;',
        '\n': ' ',
        '&amp;nbsp;': ' ',
        '&amp;#8217;': '\'',
        '&amp;#8230;': '...',
        '&amp;#8216;': '\'',
        '&amp;#8221;': '\'',
        '&amp;#8220;': '\'',
        '&amp;#8211;': '\'',
        '&amp;#8212;': '\'',
        '&amp;#38;': '&amp;',
        '&amp;#2026;': '...',
        '&amp;#160;': ' ',
        '&amp;#038;': '&amp;'
    }
    previous = raw_html
    for pattern in pairs:
        clean_text = re.sub(re.compile(pattern), pairs[pattern], previous)
        previous = clean_text
    return clean_text.strip()