I have an html. I would like to check if it contains at least one English
section. This is signified by
<summary class="section-heading"><h2 id="English">English</h2></summary>
This operation is performed millions of times. To be efficient, I want checking process stops right after it meets the first of such elements. I tried a method from here. Could you please elaborate on
why
soup.find('details[data-level="2"]:has(h2#English)')
did not work? On the other hand,soup.select_one('details[data-level="2"]:has(h2#English)')
works perfectly.how to solve it?
from bs4 import BeautifulSoup
texte = """
<div id="bodyContent" class="content mw-parser-output">
<div id="mw-content-text" style="direction: ltr;">
<h1 class="section-heading" tabindex="0" aria-haspopup="true" data-section-id="0">
<span class="mw-headline" id="title_0">pomme</span>
</h1>
<details data-level="2" open="">
<summary class="section-heading"><h2 id="English">English</h2></summary>
<details data-level="3" open="">abc</details>
</details>
<details data-level="2" open="">
<summary class="section-heading"><h2 id="French">French</h2></summary>
<details data-level="3" open="">abc</details>
</details>
</div>
</div>
"""
soup = BeautifulSoup(texte, 'html.parser')
if soup.find('details[data-level="2"]:has(h2#English)'):
print('found')
else:
print('not found')