Digital Farmer
Digital Farmer

Reputation: 2107

Using find_all in BeautifulSoup when the filter is based on two distinct elements

Currently I do it this way to pass only when there is a tf-match-analyst-verdict element inside the div which in turn should contain a class called match-header:

matches = soup.find_all('div', attrs={"class": "match-header"})
for match in matches:
    if (match.find('tf-match-analyst-verdict')):

which method is correct to pass this need in the creation of the matches object to remove the need to use if?

Upvotes: 0

Views: 29

Answers (1)

Barmar
Barmar

Reputation: 780984

Use select() instead of find_all(). Then you can use the :has() selector.

matches = soup.select('div.match-header:has(tf-match-analyst-verdict)')

:has(selector) means that the element contains a descendant that matches the selector.

Upvotes: 1

Related Questions