regular expression Python search
Question: I'm trying to extract some info from the source code in a webpage and I'm
having trouble figuring out how to go about it. Part of the source code is as
follows:

    
    
    <th>Model #:</th>
                                            <td>1561496564</td>
                                </tr>
                                                                                                                                                                       <tr>
    

I want to start at "Model #:" and go all the way up the to td>. From there, I
can erase anything that's not a number to get the 1561496564.

I can't do:

    
    
    modelMatch = re.search('Model[^\n]*', contents)
    

because the actual number is on the next line. I also can't do anything that's
not a /, d, or >. I'm thinking I can do [^\^n^:^<^/^t^h^>^r]*, but that seems
a little messy. I'm wondering if there's a better way.

For regular expression, is there an easy way to say, extract until you reach
this specific phrase of "tr"?

Thanks a lot.


Answer: You can enable re's multiline mode by pass `re.MULTILINE` parameter.

However, for tasks like extract data from a webpage, I would recommend using
tools like [lxml](http://lxml.de/),
[pyquery](https://pythonhosted.org/pyquery/),
[Beautifulsoup](http://www.crummy.com/software/BeautifulSoup/) instead. These
approaches would be much simpler and elegant.

Here's an example using pyquery module:

    
    
    In [1]: import pyquery
    
    In [2]: s = '''<th>Model #:</th>
       ...:                                         <td>1561496564</td>
       ...:                             </tr>'''
    
    In [3]: pyquery.PyQuery(s).find('td').text()
    Out[3]: '1561496564'
    

