ahmadfaraz
ahmadfaraz

Reputation: 234

How to extract text from between the <br> tags in BeautifulSoup

What i am trying to do is to scrape only the companies names from the <td> element, which has multiple <br> tags. FYI, some <td> have one company name while others have two. See <td> element below:

<td id="MainContent_DisassociatedRegistrationsCell" colspan="2">
<p style="background-color:#CCCCCC;width:100%;text-align:center">
<strong>License #: 
<a href="LicenseDetail.aspx?LicNum=332673">332673</a>
</strong>
</p>
BAY AREA REMODELING CO
<br>
5230 EAST 12TH
<br>
OAKLAND, CA 94601
<br>
<strong>Effective Dates:</strong>
09/16/1982 - 06/30/1984
<p style="background-color:#CCCCCC;width:100%;text-align:center">
<strong>License #: 
<a href="LicenseDetail.aspx?LicNum=377133">377133</a>
</strong>
</p>
SAVAGE ROOFING COMPANY
<br>
3055 ALVARADO STREET
<br>
SAN LEANDRO, CA 94577
<br>
<strong>Effective Dates:</strong>
 07/01/1982 - 03/31/1985
</td>

So from the above <td> element, i want the output:

BAY AREA REMODELING CO
SAVAGE ROOFING COMPANY

Upvotes: 0

Views: 128

Answers (2)

arnaud
arnaud

Reputation: 3483

Using BeautifulSoup:

>>> from bs4 import BeautifulSoup
>>> soup = BeautifulSoup(html, "html.parser")
>>> [p.next_sibling.strip() for p in soup.findAll("p")]
['BAY AREA REMODELING CO', 'SAVAGE ROOFING COMPANY']

Upvotes: 1

Rakesh
Rakesh

Reputation: 82785

Use next_sibling after finding the required p tag

Ex:

from bs4 import BeautifulSoup

html = """<td id="MainContent_DisassociatedRegistrationsCell" colspan="2">
<p style="background-color:#CCCCCC;width:100%;text-align:center">
<strong>License #: 
<a href="LicenseDetail.aspx?LicNum=332673">332673</a>
</strong>
</p>
BAY AREA REMODELING CO
<br>
5230 EAST 12TH
<br>
OAKLAND, CA 94601
<br>
<strong>Effective Dates:</strong>
09/16/1982 - 06/30/1984
<p style="background-color:#CCCCCC;width:100%;text-align:center">
<strong>License #: 
<a href="LicenseDetail.aspx?LicNum=377133">377133</a>
</strong>
</p>
SAVAGE ROOFING COMPANY
<br>
3055 ALVARADO STREET
<br>
SAN LEANDRO, CA 94577
<br>
<strong>Effective Dates:</strong>
 07/01/1982 - 03/31/1985
</td>"""

soup = BeautifulSoup(html, 'html.parser')
for p in soup.find_all('p'):
    print(p.next_sibling.strip())  

Output:

BAY AREA REMODELING CO
SAVAGE ROOFING COMPANY

Upvotes: 1

Related Questions