Me gustaría extraer la ruta de la imagen de un texto como este:
body = 'some text here <br> <img src="/path/to/1234/some_Random_name24.jpg" class="img-responsive" /> </br>'
O
body = '<br> Hi <img src="/path/to/15004/other_Random_name.png" class="img-responsive" /> other text'
Mi expresión regular:
match = re.search(r'src=\"(?P<path1>\"', body)
if match:
print(match.group('path1'))
else:
print("no match found")
Pero no puede capturar ningún camino. ¿Cómo puedo arreglar esto?
Para un truco rápido y sucio, podrías usar
<img[^>]*src="([^"]+)
Sin embargo, el camino dorado sería usar un analizador. Vea una demostración en regex101.com .
Python
esto podría ser
import re
junk = """body = 'some text here <br> <img src="/path/to/1234/some_Random_name24.jpg" class="img-responsive" /> </br>'
body = '<br> Hi <img src="/path/to/15004/other_Random_name.png" class="img-responsive" /> other text'"""
rx = re.compile(r'<img[^>]*src="([^"]+)')
sources = rx.findall(junk)
print(sources)
Cuyos rendimientos
['/path/to/1234/some_Random_name24.jpg', '/path/to/15004/other_Random_name.png']
Vea otra demostración en ideone.com .
Este artículo se recopila de Internet, indique la fuente cuando se vuelva a imprimir.
En caso de infracción, por favor [email protected] Eliminar
Déjame decir algunas palabras