user3896400
user3896400

Reputation: 85

Reading table using BeautifulSoup

I'm reading an HTML file with BeautifulSoup. I have a table in the HTML from which I need to read data, but the HTML contains more than one table. To distinguish between the tables, I need to see the number of columns on each line by counting <td> tags.

I count like this:

for i in soup.find_all('tr'):
    for x in i.findallnext('td'):

This returns all <td> tags after the <tr> until the end of the document. But I need to know the numbers of <td> tags between the start of a line (<tr>) and the and of that line (</tr>).

<tr> <!-- Should return 2 columns, but will return 4 in script. -->
    <td></td>
    <td></td>
</tr>
<tr>
    <td></td>
    <td></td>
</tr>

Upvotes: 0

Views: 271

Answers (1)

amow
amow

Reputation: 2223

Replace findallnext with find_all.

findallnext gives all tags after the until the end of the document as you said.

find_all gives you the child elements.

Upvotes: 3

Related Questions