Biggen
Biggen

Reputation: 295

Beautiful Soup find specific tr tags

How do I find specific tr tags within a table body? For instance consider the following:

<table cellspacing="1" cellpadding="3" class="tablehead">
   <tbody>
    <tr class="stathead">...</tr>
    <tr class="colhead">...</tr>
    <tr class="oddrow team-23-2046">...</tr>
    <tr class="evenrow team-22-1234">...</tr>
    <tr class="oddrow team-25-2326">...</tr>
    <tr class="evenrow team-25-2262">...</tr>
   </tbody>
</table>

I need all the "oddrow" and "evenrow" tags but not the "stathead" or "colhead". I was able to do something like this with a slice:

for data in soup.find_all("table", {"class": "tablehead"}):
    for row in data.find_all('tr')[2:]:
        print(row.text)

But I'm not always sure that every page where I'm scrapping that content will have this format so I'd rather explicitly search for "oddrow/evenrow". The team numbers are also different for every page so I can't use those numbers for an exact match.

Upvotes: 1

Views: 3254

Answers (1)

akuiper
akuiper

Reputation: 214967

You can try this:

soup.find("table", {"class": "tablehead"}).find_all("tr", {"class": ["oddrow", "evenrow"]})

Example:

soup = BeautifulSoup("""<table cellspacing="1" cellpadding="3" class="tablehead">
   <tbody>
    <tr class="stathead">...</tr>
    <tr class="colhead">...</tr>
    <tr class="oddrow team-23-2046">...</tr>
    <tr class="evenrow team-22-1234">...</tr>
    <tr class="oddrow team-25-2326">...</tr>
    <tr class="evenrow team-25-2262">...</tr>
   </tbody>
</table>""", "html.parser")

soup.find("table", {"class": "tablehead"}).find_all("tr", {"class": ["oddrow", "evenrow"]})
#[<tr class="oddrow team-23-2046">...</tr>,
# <tr class="evenrow team-22-1234">...</tr>,
# <tr class="oddrow team-25-2326">...</tr>,
# <tr class="evenrow team-25-2262">...</tr>]

Upvotes: 3

Related Questions