0

I want to scrape Home team and Away team from this page https://www.flashscore.com/match/hY5c1Bhh/#match-summary/match-summary

    # Get HomeTeam
    _ht = driver.find_element_by_xpath('//*[contains(@class, "home")]')
    ht = _ht.find_element_by_xpath('//*[contains(@class, "participantName")]')
    _homeName = ht.text
    
    # Get AwayTeam
    _at = driver.find_element_by_xpath('//*[contains(@class, "away")]')
    at = _at.find_element_by_xpath('//*[contains(@class, "participantName")]')
    _awayName = at.text

Output

Longford
Longford

enter image description here

2 Answers 2

1

try to store both of them in a list like this :

teams = driver.find_elements(By.CSS_SELECTOR, "div[class^='participantName'] a")
print("Home team : ", teams[0].text)
print("Away team : ", teams[1].text)
Sign up to request clarification or add additional context in comments.

12 Comments

I tried the same for scores in this page flashscore.com/match/OxfWpyjH/#match-summary/match-summary scores = driver.find_elements(By.CSS_SELECTOR, "div[class^='incidentsHeader']") _1hf = scores[0].text _2hf = scores[1].text but got IndexError: list index out of range
@luca use the same CSS_SELECTOR if you are looking to extract team names div[class^='participantName'] a same code should work.
@cruisepandey yes it works, I can extract team names. But I have to scrape scores in middle of the page as well. I tried to use CSS_SELECTOR for "div[class^='incidentsHeader']")
@luca : try this css selector div[class^='matchInfo'] div[class^='wrapper']
@cruisepandey doesn't work blank output the previous code Index out of range
|
0

You are missing the . when trying to locate element inside other element.
So your code should be

# Get HomeTeam
    _ht = driver.find_element_by_xpath('//*[contains(@class, "home")]')
    ht = _ht.find_element_by_xpath('.//*[contains(@class, "participantName")]')
    _homeName = ht.text
    
    # Get AwayTeam
    _at = driver.find_element_by_xpath('//*[contains(@class, "away")]')
    at = _at.find_element_by_xpath('.//*[contains(@class, "participantName")]')
    _awayName = at.text

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.