Python: HTML regex not matching
I have this code:
reg = re.search('<div class="col result_name">(.*开发者_开发知识库)</div>', html)
print 'Value is', reg.group()
Where 'html' contains something like this:
<div class="col result_name">
<h4>Blah</h4>
<p>
blah
</p>
</div>
But it's not returning anything.
Value is
Traceback (most recent call last):
File "run.py", line 37, in <module>
print 'Value is', reg.group()
Don't use regex to parse html. Use a html parser
import lxml.html
doc = lxml.html.fromstring(your_html)
result = doc.xpath("//div[@class='col result_name']")
print result
Obligatory link:
RegEx match open tags except XHTML self-contained tags
The dot does not neccessarily match newlines in REs, you need the DOTALL
flag (?s)
for that.
http://docs.python.org/library/re.html :
The special characters are:
'.' (Dot.) In the default mode, this matches any character except a newline. If the DOTALL flag has been specified, this matches any character including a newline.
精彩评论