parsing with expression including a newline in python
Question: I was trying to parsing some html files, in which I want to extract some value
called "Total Cash", but these html come in two different forms:

  1. `...Total Cash (mrq):</td> <td class="yfnc_tabledata1">8.71B</td>...`

  2. `...Total Cash (mrq):</td> <td class="yfnc_tabledata1">8.71B</td>...`

It is easy to parsing the first one, and the following code gives me the
number 8.71B

    
    
    source.split('Total Cash (mrq):</td> <td class="yfnc_tabledata1">')[1].split('</td>')[0]
    

However, I don't know how to parse the second form, in which the value and the
string 'Total Cash (mrq)' are in two different lines. Any suggestions? Also,
there are about 9000 htmls, and each file contains about 1000 rows of code.


Answer: You can try something like this as you mentioned `beautifulsoup`

    
    
    from bs4 import BeautifulSoup
    
    html_doc = """
    <td>Total Cash (mrq):</td>
    <td class="yfnc_tabledata1">8.71B</td>
    """
    
    soup = BeautifulSoup(html_doc, 'html.parser')
    
    td_head = soup.find(text="Total Cash (mrq):").parent
    td_desired = td_head.find_next('td')
    
    print td_desired.contents[0]
    

If you need to get all the elements you can try `find_all` , by using
something like this :

    
    
    td_heads_content = soup.find_all(text="Total Cash (mrq):")
    for elem in td_heads_content:
        td_head = elem.parent
    

