I’ve tried to get the text from class="eventAwayMinute">57
in every matchEvent
class (Parent tag)
If a matchEvent
class contains class="eventIcon eventIcon_1"
:
JavaScript
x
11
11
1
<div class="matchEvent">
2
<div class="eventHomePlayer">
3
</div>
4
<div class="eventHomeMinute"></div>
5
<div class="eventIcon eventIcon_1"></div>
6
<div class="eventAwayMinute">57'</div>
7
<div class="eventAwayPlayer">
8
George
9
<span>(Irakli)</span> </div>
10
</div>
11
I tried
JavaScript
1
2
1
Minutes = [(gm.get_text()).strip() for gm in soup.select('matchEvent , div[class$="eventIcon_1"]')]
2
and it dose not work.
I tried also
JavaScript
1
2
1
Minutes = [(gm.get_text()).strip() for gm in soup.select('matchEvent')]
2
But it returns all minutes that exist in every matchEvent
(There is several matchEvent
classes in html code).
Advertisement
Answer
You can use the :has()
CSS Selector to check if matchEvent
has an eventIcon eventIcon_1
class, and than print the eventAwayMinute
class:
JavaScript
1
18
18
1
from bs4 import BeautifulSoup
2
3
html = """<div class="matchEvent">
4
<div class="eventHomePlayer">
5
</div>
6
<div class="eventHomeMinute"></div>
7
<div class="eventIcon eventIcon_1"></div>
8
<div class="eventAwayMinute">57'</div>
9
<div class="eventAwayPlayer">
10
George
11
<span>(Irakli)</span> </div>
12
</div>
13
"""
14
soup = BeautifulSoup(html, "html.parser")
15
16
for tag in soup.select(".matchEvent:has(.eventIcon.eventIcon_1)"):
17
print(tag.select_one(".eventAwayMinute").text.strip("'"))
18
Output:
JavaScript
1
2
1
57
2