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?

>Solution :

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

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

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

Leave a Reply Cancel reply