Skip to content Skip to sidebar Skip to footer

Regex For Search And Get The Src Of A Image

Assume I am having a html string containing the following code snippet. ... ... I want to sear

Solution 1:

Using Regular Expression :

>>> import re
>>> str =  '<img class="employee thumb" src="http://localhost/services/employee1.jpg" />'
>>> if re.search('img class="employee thumb"', str):
...     print re.findall ( 'src="(.*?)"', s, re.DOTALL)
... 
['http://localhost/services/employee1.jpg']

Using lxml :

>>> from lxml import etree
>>> root = etree.fromstring("""
... <html>
...     <img class="employee thumb" src="http://localhost/services/employee1.jpg" />
... </html>
... """)
>>> print root.xpath("//img[@class='employee thumb']/@*")[1]
http://localhost/services/employee1.jpg

Post a Comment for "Regex For Search And Get The Src Of A Image"