# -*- coding: utf-8 -*-
import re
import sys

from lxml import html

rex1 = re.compile(r'(?<=\n)( )+')
rex2 = re.compile(r'^[ \t\n\r\f]*')
rex3 = re.compile(r'[ \t]+(?=([ \t]))')
rex4 = re.compile(r'[ \n]+(?=(\n))')
rex5 = re.compile(r'[ \n]*$')


class Cleaner:
    """The Cleaner-Class tries to get the raw extracted text of the extractors
    in a comparable format. For this it deletes unnecessary whitespaces
    or in case of readability html-tags which are still in the extracted
    text.
    """

    def delete_tags(self, arg):
        """Removes html-tags from extracted data.

        :param arg: A string, the string which shall be cleaned
        :return: A string, the cleaned string
        """

        if len(arg) > 0:
            raw = html.fromstring(arg)
            return raw.text_content().strip()

        return arg

    def delete_whitespaces(self, arg):
        """Removes newlines, tabs and whitespaces at the beginning, the end and if there is more than one.

        :param arg: A string, the string which shell be cleaned
        :return: A string, the cleaned string
        """
        # Deletes whitespaces after a newline
        arg = re.sub(rex1, '', arg)
        # Deletes every whitespace, tabulator, newline at the beginning of the string
        arg = re.sub(rex2, '', arg)
        # Deletes whitespace or tabulator if followed by whitespace or tabulator
        arg = re.sub(rex3, '', arg)
        #  Deletes newline if it is followed by an other one
        arg = re.sub(rex4, '', arg)
        # Deletes newlines and whitespaces at the end of the string
        arg = re.sub(rex5, '', arg)
        return arg

    def do_cleaning(self, arg):
        """Does the actual cleaning by using the delete methods above.

        :param arg: A string, the string which shell be cleaned. Or a list, in which case each of the strings within the
        list is cleaned.
        :return: A string, the cleaned string. Or a list with cleaned string entries.
        """
        if arg is not None:
            if isinstance(arg, list):
                newlist = []
                for entry in arg:
                    newlist.append(self.do_cleaning(entry))
                return newlist
            else:
                if sys.version_info[0] < 3:
                    arg = unicode(arg)
                else:
                    arg = str(arg)
                arg = self.delete_tags(arg)
                arg = self.delete_whitespaces(arg)
                return arg
        else:
            return None

    def clean(self, list_article_candidates):
        """Iterates over each article_candidate and cleans every extracted data.

        :param list_article_candidates: A list, the list of ArticleCandidate-Objects which have been extracted
        :return: A list, the list with the cleaned ArticleCandidate-Objects
        """
        # Save cleaned article_candidates in results.
        results = []

        for article_candidate in list_article_candidates:
            article_candidate.title = self.do_cleaning(article_candidate.title)
            article_candidate.description = self.do_cleaning(article_candidate.description)
            article_candidate.text = self.do_cleaning(article_candidate.text)
            article_candidate.topimage = self.do_cleaning(article_candidate.topimage)
            article_candidate.author = self.do_cleaning(article_candidate.author)
            article_candidate.publish_date = self.do_cleaning(article_candidate.publish_date)

            results.append(article_candidate)

        return results

@@ -64,18 +64,28 @@
 html%0A%0Are
-x1
+_newline_spc
  = re.co
@@ -108,18 +108,34 @@
  )+')%0Are
-x2
+_starting_whitespc
  = re.co
@@ -160,18 +160,30 @@
 f%5D*')%0Are
-x3
+_multi_spc_tab
  = re.co
@@ -212,18 +212,31 @@
 %5D))')%0Are
-x4
+_double_newline
  = re.co
@@ -262,18 +262,35 @@
 n))')%0Are
-x5
+_ending_spc_newline
  = re.co
@@ -1246,10 +1246,20 @@
 b(re
-x1
+_newline_spc
 , ''
@@ -1378,10 +1378,26 @@
 b(re
-x2
+_starting_whitespc
 , ''
@@ -1511,10 +1511,22 @@
 b(re
-x3
+_multi_spc_tab
 , ''
@@ -1620,10 +1620,23 @@
 b(re
-x4
+_double_newline
 , ''
@@ -1737,10 +1737,27 @@
 b(re
-x5
+_ending_spc_newline
 , ''

Improve regex naming