1

i have a problem with my html parser. I convert emails filled with html code into nice clean text, except for the "< style > content < /style >" part, which it completely ignores and i have no clue as to what i did wrong:

    # Remove any HTML code from our raw content
htmlparse = re.sub(r'<.*?>', '', clean) \
    .replace("&nbsp;", '') \
    .replace("é", 'é') \
    .replace("è", 'è') \
clean_email = htmlparse

what it actually should delete is:

<style>      .MailHeader      {          font: normal 10pt Tahoma, Verdana, Sans-Serif;          vertical-align: top;          padding-bottom: 0px;          padding-top: 0px;          spacing: 0px 0px 0px 0px;      }      .DataHeader      {          font-family: Tahoma;          font-size: 8pt;          color: #666666;          text-decoration: none;          padding-left: 15px;          padding-right: 15px;          border: solid 1px #E0E0E0;          vertical-align: text-top;      }      .Data      {          font: normal 8pt Tahoma,Verdana;          padding-left: 3px;          padding-right: 15px;          border: solid 1px #E0E0E0;          background: #F9F9F9;          font-size: 8pt;          color: #666666;          height: 20px !important;      }      .GridHeader      {          font: normal 8pt Tahoma,Verdana;          padding-left: 6px;          background: #DAEBFF;          height: 20px;      }      .DataRow      {          padding-left: 3px;          padding-right: 15px;          border: solid 1px #E0E0E0;          font-size: 8pt;          color: #003399;      }      .GridRow      {          font: normal 8pt Tahoma, Verdana, Sans-serif;          padding-left: 6px;          background: transparent;          height: 20px !important;          min-height: 1%;      }      .GridAltRow      {          font: normal 8pt Tahoma, Verdana, Sans-serif;          padding-left: 6px;          background: #F9F9F9;          height: 20px !important;          min-height: 1%;      }      .li      {          font: normal 10pt Tahoma, Verdana, Sans-Serif;          vertical-align: top;          padding-left: 5px;      }      .TableHeader      {          font-family: Tahoma,calibri,verdana;          font-size: 8pt;          font-weight: bold;          height: 22px;          color: #003399;          border: solid 1px #E0E0E0;          border-collapse: collapse;          padding-left: 5px;          padding-right: 5px;          background-color: #BBD8FF;      }      .TableSubHeader      {          font: normal 8pt Tahoma, Verdana, Sans-Serif;          vertical-align: middle;          padding-left: 3px;          font-weight: bold;          color: #666666;      }      .Separator      {          background-repeat: repeat-x;          background-position: center;          background: #666666;      }      .tableDetail      {          padding: 0 0 0 0;          spacing: 0 0 0 0;          border-collapse: collapse;          width: 750px;          margin-left: 5px;          border: solid 1px #E0E0E0;      }      .style1      {          font: normal 10pt Tahoma, Verdana, Sans-Serif;          vertical-align: top;          padding-bottom: 0px;          padding-top: 0px;          spacing: 0px 0px 0px 0px;          height: 18px;      }  </style>

What it actually does is delete style and /style but leaves the whole garbage of the stylesheet in the parsed file...

.MailHeader { font: normal 10pt Tahoma, Verdana, Sans-Serif; vertical-align: top; padding-bottom: 0px; padding-top: 0px; spacing: 0px 0px 0px 0px; } .DataHeader { font-family: Tahoma; font-size: 8pt; color: #666666; text-decoration: none; padding-left: 15px; padding-right: 15px; border: solid 1px #E0E0E0; vertical-align: text-top; } .Data { font: normal 8pt Tahoma,Verdana; padding-left: 3px; padding-right: 15px; border: solid 1px #E0E0E0; background: #F9F9F9; font-size: 8pt; color: #666666; height: 20px !important; } .GridHeader { font: normal 8pt Tahoma,Verdana; padding-left: 6px; background: #DAEBFF; height: 20px; } .DataRow { padding-left: 3px; padding-right: 15px; border: solid 1px #E0E0E0; font-size: 8pt; color: #003399; } .GridRow { font: normal 8pt Tahoma, Verdana, Sans-serif; padding-left: 6px; background: transparent; height: 20px !important; min-height: 1%; } .GridAltRow { font: normal 8pt Tahoma, Verdana, Sans-serif; padding-left: 6px; background: #F9F9F9; height: 20px !important; min-height: 1%; } .li { font: normal 10pt Tahoma, Verdana, Sans-Serif; vertical-align: top; padding-left: 5px; } .TableHeader { font-family: Tahoma,calibri,verdana; font-size: 8pt; font-weight: bold; height: 22px; color: #003399; border: solid 1px #E0E0E0; border-collapse: collapse; padding-left: 5px; padding-right: 5px; background-color: #BBD8FF; } .TableSubHeader { font: normal 8pt Tahoma, Verdana, Sans-Serif; vertical-align: middle; padding-left: 3px; font-weight: bold; color: #666666; } .Separator { background-repeat: repeat-x; background-position: center; background: #666666; } .tableDetail { padding: 0 0 0 0; spacing: 0 0 0 0; border-collapse: collapse; width: 750px; margin-left: 5px; border: solid 1px #E0E0E0; } .style1 { font: normal 10pt Tahoma, Verdana, Sans-Serif; vertical-align: top; padding-bottom: 0px; padding-top: 0px; spacing: 0px 0px 0px 0px; height: 18px; } Hello, this is a test mail.

Can anyone help me?

thank you guys in advance, regards

2
  • Obligatory: stackoverflow.com/a/1732454/502381 Commented Apr 8, 2015 at 8:14
  • 2
    But more seriously, wouldn't it be easier to use a HTML parsing library? Commented Apr 8, 2015 at 8:14

2 Answers 2

1

First remove the style itself, and then in second pass, do what you want to do.

import re

some = """
<style>.MailHeader { font: normal 10pt Tahoma, Verdana, Sans-Serif;
vertical-align: top; padding-bottom: 0px; padding-top: 0px; spacing: 0px 0px 0px 0px; } 
.DataHeader { font-family: Tahoma; font-size: 8pt; color: #666666; text-decoration: none;
padding-left: 15px; padding-right: 15px; border: solid 1px #E0E0E0; vertical-align: text-top; } 
.Data { font: normal 8pt Tahoma,Verdana; padding-left: 3px; padding-right: 15px; border: solid 1px #E0E0E0;
\ background: #F9F9F9; font-size: 8pt; color: #666666; height: 20px !important; } 
.GridHeader { font: normal 8pt Tahoma,Verdana; padding-left: 6px; background: #DAEBFF; height: 20px; }
.DataRow { padding-left: 3px; padding-right: 15px; border: solid 1px #E0E0E0; font-size: 8pt; color: #003399; }
.GridRow { font: normal 8pt Tahoma, Verdana, Sans-serif; padding-left: 6px; background: transparent; 
height: 20px !important; min-height: 1%; } .GridAltRow { font: normal 8pt Tahoma, Verdana, Sans-serif;
padding-left: 6px; background: #F9F9F9; height: 20px !important; min-height: 1%; } 
.li { font: normal 10pt Tahoma, Verdana, Sans-Serif; vertical-align: top; padding-left: 5px; }
.TableHeader { font-family: Tahoma,calibri,verdana; font-size: 8pt; font-weight: bold; height: 22px;
color: #003399; border: solid 1px #E0E0E0; border-collapse: collapse; padding-left: 5px; 
padding-right: 5px; background-color: #BBD8FF; } 
.TableSubHeader { font: normal 8pt Tahoma, Verdana, Sans-Serif;
vertical-align: middle; padding-left: 3px; font-weight: bold; color: #666666; }
.Separator { background-repeat: repeat-x; background-position: center; background: #666666; }
.tableDetail { padding: 0 0 0 0; 
spacing: 0 0 0 0; border-collapse: collapse; width: 750px; margin-left: 5px; border: solid 1px #E0E0E0; }
.style1 { font: normal 10pt Tahoma, Verdana, Sans-Serif; vertical-align: top; padding-bottom:
0px; padding-top: 0px; spacing: 0px 0px 0px 0px; height: 18px; }
</style>
<h1>Hello, this is a test mail.</h1>
"""

some1 = re.sub(r'<style>.*</style>', '', some, flags=re.DOTALL)

print some1

Result :

I have no name!@sla-334:~/stack_o$ python stack_o_html.py 


<h1>Hello, this is a test mail.</h1>

Now, do what you want to do with this html.

Sign up to request clarification or add additional context in comments.

Comments

1

I kinda fixed it myself by reparsing trough the parsed text and using this as parameters

cleaner = re.sub(r'{.*}', '', clean_email)\
      .replace(".MailHeader", '') \

Ill try out your solutions

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.