Zeno
Zeno

Reputation: 1839

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()

Upvotes: 0

Views: 918

Answers (3)

nosklo
nosklo

Reputation: 223122

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

Upvotes: 6

Thom Wiggers
Thom Wiggers

Reputation: 7052

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.

Upvotes: 2

Ulrich Schwarz
Ulrich Schwarz

Reputation: 7727

The dot does not neccessarily match newlines in REs, you need the DOTALL flag (?s) for that.

Upvotes: 3

Related Questions