Найти - Пользователи
Полная версия: регулярные выражения - обрезать html теги
Начало » Python для новичков » регулярные выражения - обрезать html теги
1
mro
нужно из куска хтмл кода выделить текст (key-value)
## -*- coding: UTF-8 -*-
#
import re

html = """
<table style="background-color: #ffffff; font-size: x-small; color: #000070; font-family: Verdana;">
<tr>
<td style="background-color: #c3d7f0; width: 150;">
<b>Marca:</b>
</td>
<td style="background-color: #ffffcc; font-size: small;">Imprensa - OPTICA CENTRAL</td>
</tr>
<tr>
<td style="background-color: #c3d7f0;">
<b>Sub-Marca:</b>
</td>
<td> </td>
</tr>
<tr>
<td style="background-color: #c3d7f0;">
<b>Descri&#231;&#227;o:</b>
</td>
<td>CHEQUE OFERTA DE 25E</td>
</tr>
<tr>
<td style="background-color: #c3d7f0;">
<b>Sector:</b>
</td>
<td>OPTICA</td>
</tr>
<tr>
<td style="background-color: #c3d7f0;">
<b>Categoria:</b>
</td>
<td>LOJAS DE OPTICA</td>
</tr>
......................
<tr>
<td style="background-color: #c3d7f0;">
<b>Mancha:</b>
</td>
<td>PAG.DUPLA</td>
</tr>
</table>"""

pattern = '(?<=<td>).*(?=</td>)|(?<=<b>).*(?=</b>)'

print re.findall(pattern, html, re.UNICODE | re.IGNORECASE)
результат
['Marca:', 'Sub-Marca:', '    ', 'Descri\xc3\xa7\xc3\xa3o:', 'CHEQUE OFERTA DE 25E', 'Sector:', 'OPTICA', 'Categoria:', 'LOJAS DE OPTICA',
'Classe:', 'Geral', 'Sub-Classe:', 'Geral', 'Sit. Camp:', 'Novo Anuncio', 'Publica\xc3\xa7\xc3\xa3o:', 'VIP', 'Data Publica\xc3\xa7\xc3\xa3o:',
'2012/2/28', 'Caderno:', 'PRINCIPAL', 'Mancha:', 'PAG.DUPLA']
но не находит этот кусок(второй по счету)
<td style="background-color: #ffffcc; font-size: small;">Imprensa - OPTICA CENTRAL</td>
Mozart
Наверное потому что в паттерне стоит от <td>, а в шаблоне от <td …>, т.е. без закрывающей стрелочки >

re.compile(r"<td\sstyle=.*>(.*)</td>", re.S | re.I)

#['Imprensa - OPTICA CENTRAL']
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Powered by DjangoBB