I have a page with some tables in its source:
<table width='100%' cellspacing='0' cellpadding='2' class='an'>
<tr>
<td width='35%' align='right'>XXX :</td>
<td><b>20</b></td>
</tr>
<tr><
td align='right'>XXX :</td>
<td><b>XXX</b></td>
</tr>
<tr>
<td align='right'>XX :</td>
<td><b>XXX</b></td>
</tr>
<tr>
<td align='right'>XXX :</td>
<td><b>XXX</b></td>
</tr>
<tr>
<td align='right'>XXX :</td>
<td><b>XXX</b></td>
</tr>
<tr>
<td align='right'>XXX :</td>
<td><b>XXX</b></td>
</tr>
<tr>
<td align='right'>XXX :</td>
<td><b>XXX</b></td>
</tr>
</table>
<table width='361' cellspacing='0' cellpadding='2' class='an'>
<tr>
<td width='35%' align='right'>XXX :</td>
<td><b>XXX</b></td>
</tr>
<tr>
<td align='right'>XXX :</td>
<td><b>XXX</b></td>
</tr>
<tr>
<td align='right'>XXX :</td>
<td><b>XXX</b></td>
</tr>
<tr>
<td align='right'>XXX :</td>
<td><b>XXX</b></td>
</tr>
<tr>
<td align='right'>XX :</td>
<td><a href='XXX'><b>XXX</b></a></td>
</tr>
<tr>
<td align='right'>PHONE :</td>
<td><b>518878943</b></td>
</tr>
</table>
I would like to get from this page a phone number, from the second table:
<td align='right'>PHONE :</td>
<td><b>518878943</b></td>
However, my code:
page_src="""<table width='100%' cellspacing='0' cellpadding='2' class='an'>
<tr>
<td width='35%' align='right'>XXX :</td>
<td><b>20</b></td>
</tr>
<tr><
td align='right'>XXX :</td>
<td><b>XXX</b></td>
</tr>
<tr>
<td align='right'>XX :</td>
<td><b>XXX</b></td>
</tr>
<tr>
<td align='right'>XXX :</td>
<td><b>XXX</b></td>
</tr>
<tr>
<td align='right'>XXX :</td>
<td><b>XXX</b></td>
</tr>
<tr>
<td align='right'>XXX :</td>
<td><b>XXX</b></td>
</tr>
<tr>
<td align='right'>XXX :</td>
<td><b>XXX</b></td>
</tr>
</table>
<table width='361' cellspacing='0' cellpadding='2' class='an'>
<tr>
<td width='35%' align='right'>XXX :</td>
<td><b>XXX</b></td>
</tr>
<tr>
<td align='right'>XXX :</td>
<td><b>XXX</b></td>
</tr>
<tr>
<td align='right'>XXX :</td>
<td><b>XXX</b></td>
</tr>
<tr>
<td align='right'>XXX :</td>
<td><b>XXX</b></td>
</tr>
<tr>
<td align='right'>XX :</td>
<td><a href='XXX'><b>XXX</b></a></td>
</tr>
<tr>
<td align='right'>PHONE :</td>
<td><b>518878943</b></td>
</tr>
</table>
"""
soup = BeautifulSoup(page_src, 'html.parser')
divs = soup.findAll("table", {"class": "an"})
for div in divs:
row = ''
rows = [row in div.findAll('tbody').findAll('tr')]
Gives me such an error message:
Traceback (most recent call last):
File "test.py", line 198, in <module>
rows = [row in div.findAll('tbody').findAll('tr')]
AttributeError: 'ResultSet' object has no attribute 'findAll'
How to solve this and get the phone number from the page? Thanks
EDIT:
Partly solved. Partly, because I think my solution is ugly, but works. Maybe someone will come up with prettier solution?
tds = []
soup = BeautifulSoup(page_src, 'html.parser')
divs = soup.findAll("table", {"class": "an"})
for div in divs:
rows = div.findAll('tr')
for row in rows :
tds.append(row.findAll('td'))
phone = str(tds[12][1])
phone = phone.replace("<td><b>", "").replace("</b></td>", "").strip()
print phone
Beautiful Soup provides "find()" and "find_all()" functions to get the specific data from the HTML file by putting the specific tag in the function. find() function - return the first element of given tag. find_all() function - return the all the element of given tag.
Find the td
element containing PHONE :
and then get the following sibling element. One line:
soup.find("td", text="PHONE :").find_next_sibling("td").text
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With