import sys
import re


def main():
    _, dirty_file = sys.argv
    text = ''

    with open(dirty_file) as f:
        text = f.read()

    # remove the multitude of useless spans
    text = replace_regex(r'<\/?span[^>]*>', '', text)

    #remove &nbsp;
    text = replace_regex(r'&nbsp;', ' ', text)

    # put headings on one line
    text = replace_regex(r'<h([1-5])>\n([^<]*)<\/h\1>',
                         r'\n<h\1>\2</h\1>', text)

    # remove class blob-wrapper divs
    text = replace_regex(r'(<div class="blob-wrapper[^>]*>)'
                         '(.*?(?=<\/div>))(<\/div>)', r'\2', text)

    # remove div encapsulated brs
    text = replace_regex(r'<div>\s*<br \/>\s*<\/div>', '</ br>', text)

    # remove empty tags eg <pre></pre>
    text = replace_regex(r'<([^>]*)>\s*<\/\1>', '', text)

    with open(dirty_file + '_cleaned', 'w+') as f:
        f.write(fix_formatting(text))


def fix_formatting(text):
    """Fixes aesthetic formatting of text"""

    # make sure <br \> tags have one line above and below
    text = replace_regex(r'(\s*<br \/>\s*)', r'\n\1\n', text)

    # remove multiple \n
    text = replace_regex(r'\n{2,}', r'\n', text)

    return text


def replace_regex(regex, repl, text):
    """Replaces all occurances of a regex pattern regex, in text"""
    replacer = re.compile(regex, re.DOTALL)
    replaced = replacer.sub(repl, text)

    return replaced

if __name__ == '__main__':
    main()

@@ -457,21 +457,8 @@
 ass 
-blob-wrapper 
 divs
@@ -489,100 +489,131 @@
 x(r'
-(%3Cdiv class=%22blob-wrapper%5B%5E%3E%5D*%3E)'%0A                         '(.*?(?=%3C%5C/div%3E))(
+%3C%5C/?div%5B%5E%3E%5D*%3E', '', text)%0A%0A    # remove div encapsulated brs%0A    text = replace_regex(r'%3Cdiv%3E%5Cs*%3Cbr %5C/%3E%5Cs*
 %3C%5C/div%3E
-)
 ', 
-r'%5C2
+'%3C/ br%3E
 ', t
@@ -627,35 +627,37 @@
    #
- 
 remove 
-div encapsulated br
+brs within heading tag
 s%0A  
@@ -686,41 +686,84 @@
 (r'%3C
-div
+h(%5B1-5%5D)
 %3E%5Cs*
+(
 %3Cbr %5C/%3E
-%5Cs*%3C%5C/div%3E', '%3C/ br
+)(%5B%5E%3C%5D*)%3C%5C/h%5C1%3E',%0A                        r'%3Ch%5C1%3E%5C3%3C/h%5C1
 %3E', 

Change to remove all divs and add removing brs within header tags