FindAll in Selenium scrapping w/ Python

Issue

Im having a problem in scrapping a soccer team name with selenium. The problem is that the span class about the home team and away team is the same ("Nh")

<span class="Lh">
       <span class="Nh" id="0-639478__match-row__home-team-name">
        Estudiantes Merida
       </span>
      </span>

<span class="Mh">
       <span class="Nh" id="0-639478__match-row__away-team-name">
        Universidad Central
       </span>
      </span> 

How do i get the output : Home Team vs Away Team ? In this case is : Estudiantes Merida vs Universidad Central

I tried this :

teams_home = soup.find_all(‘span’, class_="Nh") print(teams_home)

i tried to find by ID too, but didnt work too. Any help please ?

URL : https://www.livescores.com/football/live/?tz=-3, editing sry i forgot

Solution

Your title says you are using Selenium in your title but your code snippet uses soup which leads me to believe you are using Beautiful Soup. Either way however, it looks like your best bet would be to get the element with class name "Lh" and the element with the class name "Mh" then from there get the child’s text.

If using Selenium this worked for me:

driver.get('https://www.livescores.com/football/live/?tz=-3')
WebDriverWait(driver, 60).until(lambda d: d.find_elements(By.CLASS_NAME, 'Lh'))
first = driver.find_elements(By.CLASS_NAME, 'Lh')
second = driver.find_elements(By.CLASS_NAME, 'Mh')

for i, element in enumerate(first):
  print(first[i].text + ' vs ' + second[i].text)

If using BeautifulSoup this also worked for me:

page = requests.get('https://www.livescores.com/football/live/?tz=-3')
soup = BeautifulSoup(page.content, 'html.parser')
first = soup.find_all(class_='Lh')
second = soup.find_all(class_='Mh')

for i, element in enumerate(first):
  print(first[i].text + ' vs ' + second[i].text)

Both gave output:

CA Huracan vs Rosario Central
Independiente vs Talleres
Chapecoense AF vs Criciuma
ASC San Diego vs California United

Answered By – Connor Dudley

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published