Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading