user1592380
user1592380

Reputation: 36317

Union of 2 sets with beautiful soup select

I have the following html from a table with alternating light and dark rows:

<tr class="light">
    <td>....
    </td>
  </tr>

  <tr class="dark">
    <td>....

I can get either all the light or all dark rows using:

soup.select('tr.light') or soup.select('tr.dark')

Is there a way to combine both in a single statement?

Upvotes: 1

Views: 439

Answers (1)

Gerrat
Gerrat

Reputation: 29710

Beautiful soup supports CSS selectors, so it should just allow you to use grouping.

If it does, then you could just select both via:

soup.select('tr.dark, tr.light')  # note the comma between.

EDIT: Beautiful soup seems to have rather limited CSS selector support, so alternatively you could try this:

import re
soup.find_all("td" class_=re.compile("light|dark"))

Another alternative is that lxml is supposed to support the full range of CSS selectors.

Upvotes: 1

Related Questions