0

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()
Zeno
  • 1,769
  • 7
  • 33
  • 61

3 Answers3

6

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

Community
  • 1
  • 1
nosklo
  • 217,122
  • 57
  • 293
  • 297
  • I'm getting results like this: [, – Zeno Jan 10 '11 at 18:55
  • @Zeno: Yeah, those are all the divs lxml found in your html. The elements. You can print them, or do further parsing with them. For example, try this: `for onediv in result: print lxml.html.tostring(onediv, pretty_print=True)` – nosklo Jan 10 '11 at 19:27
  • Does xpath support regex? I want to do something like (col|row) in there. – Zeno Jan 10 '11 at 21:54
3

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

Ulrich Schwarz
  • 7,598
  • 1
  • 36
  • 48
2

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.

Thom Wiggers
  • 6,938
  • 1
  • 39
  • 65