arcee123 arcee123 - 3 months ago 13
Python Question

using beautifulsoup to get second and third child in BeautifuSoup

I have a table similar to below:

<table>
<thead>
<tr>
<th></th>
<th></th>
<th></th>
<th></th>
<tr>
</thead>
<tbody>
<tr>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
</tr>
</tbody>
</table>


I need the second and third of each row. I am using BeautifulSoup in Python 3.5

Right now, I'm doing:

Table = Soup.find('table', attrs={'id': 'field'})
Tbody = Table.find('tbody')
Records = Tbody.find_all('tr')
Record = Records.find_all('td')
for field in Records:
print (Record[2].text)


I am receiving the error:

Traceback (most recent call last):
File "C:/Users/arcee/PycharmProjects/scraper/main.py", line 33, in <module>
Record = Records.find_all('td')
AttributeError: 'ResultSet' object has no attribute 'find_all'


Is there an easier way to get the Second and third TD elements?

Thanks

Answer

you're getting that error because of these lines:

Records = Tbody.find_all('tr')
Record = Records.find_all('td')

find_all returns a list (ResultSet) of occurrences of the searched tag. The ResultSet doesn't have a find_all attribute - but each individual member of it does. so, doing..

Records = Tbody.find_all('tr')
for record in records:
    print(record.find_all('td'))

will make your code run.