Issue
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
Solution
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
Answered By - Rakesh
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.