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"
:
<div class="matchEvent"> <div class="eventHomePlayer"> </div> <div class="eventHomeMinute"></div> <div class="eventIcon eventIcon_1"></div> <div class="eventAwayMinute">57'</div> <div class="eventAwayPlayer"> George <span>(Irakli)</span> </div> </div>
I tried
Minutes = [(gm.get_text()).strip() for gm in soup.select('matchEvent , div[class$="eventIcon_1"]')]
and it dose not work.
I tried also
Minutes = [(gm.get_text()).strip() for gm in soup.select('matchEvent')]
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:
from bs4 import BeautifulSoup html = """<div class="matchEvent"> <div class="eventHomePlayer"> </div> <div class="eventHomeMinute"></div> <div class="eventIcon eventIcon_1"></div> <div class="eventAwayMinute">57'</div> <div class="eventAwayPlayer"> George <span>(Irakli)</span> </div> </div> """ soup = BeautifulSoup(html, "html.parser") for tag in soup.select(".matchEvent:has(.eventIcon.eventIcon_1)"): print(tag.select_one(".eventAwayMinute").text.strip("'"))
Output:
57