Reputation: 163
I'm trying to parse the table located at this page http://www.bluenote.net/newyork/schedule/index.shtml. I am trying to get the start time and Band name of who is playing on the current today. I noticed that the first td that contains the p element in the table is what I'm looking for. Any idea how I would go about this? I tried using
soup.findAll("p")
but I am trying to select the entire "td" that contains that "p" element.
Upvotes: 1
Views: 819
Reputation: 476584
You can use a generator:
first_td = next(td for td in soup.find_all('td') if td.p)
# ^ generator ^
The find_all
will yield all <td>
tags. We then filter the tags by td.p
. This will return the p
tag (given there is one). Otherwise it will return None
. Since the truthiness of None
is False
and for the tag objects it is True
, the generator will thus enumerate all <td>
tags with a <p>
tag.
We call next(..)
to obtain the first of these elements. In case no such element exists, it will raise a StopIteration
exception.
In case you want all these tags, you can use list comprehension:
[td for td in soup.find_all('td') if td.p]
Upvotes: 2