1

Utilizando python, qual seria a maneira fácil de limpar os parâmetros de tag oriundos de ferramentas da microsoft?

Inicialmente estou tentando transformar via beautiful soup, mas estou aberto a todas as sugestões! :D

Desta forma:

<p style="text-decoration: underline;">Hello <strong>World!</strong></p>
<p style="color: #228;">How are you today?</p>
<table style="width: 300px; text-align: center;" border="1" cellpadding="5">
<tr>
<th width="75"><strong><em>Name</em></strong></th>
<th colspan="2"><span style="font-weight: bold;">Telephone</span></th>
</tr>
<tr>
<td>John</td>
<td><a style="color: #F00; font-weight: bold;" href="tel:0123456785">0123 456 785</a></td>
<td><img width="25" height="30" src="images/check.gif" alt="checked" /></td>
</tr>
</table>

Para esta forma:

<p>Hello <strong>World!</strong></p>
<p>How are you today?</p>
<table border="1" cellpadding="5">
<tr>
<th width="75"><strong><em>Name</em></strong></th>
<th colspan="2"><span>Telephone</span></th>
</tr>
<tr>
<td>John</td>
<td><a href="tel:0123456785">0123 456 785</a></td>
<td><img width="25" height="30" src="images/check.gif" alt="checked" /></td>
</tr>
</table>
2
  • Consegue colocar sua tentativa com o beautiful soup? Aliás, basicamente o que você precisa é remover as propriedades style?
    – Woss
    22/02/2018 às 9:55
  • Sim. Remover todas elas.
    – britodfbr
    22/02/2018 às 10:53

1 Resposta 1

3

Pode usar o re.sub()

Exemplo para remover os atributos style:

import re

html_string = "[coloque aqui seu HTML]"
html_no_style = re.sub(r' style="[^"]+"', '', html_string)

É importante fazer testes com vários arquivos HTML diferentes para saber se não vai precisar melhorar a Regex de captura.

Você deve fazer log-in para responder a esta pergunta.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .