Assume I am having a html string containing the following code snippet.
... ...
I want to search whether this tag is available and if so get the src url.
How to do this in python?
解决方案
Using Regular Expression :
>>> import re
>>> str = ''
>>> 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("""
...
...
...
... """)
>>> print root.xpath("//img[@class='employee thumb']/@*")[1]
http://localhost/services/employee1.jpg