Home > Mobile >  python selenium- how to get only some of the information inside a <a> HTML element?
python selenium- how to get only some of the information inside a <a> HTML element?

Time:11-18

The website bellow will she the scores of a all the soccer matches and this one is an example, im trying to get the teams that have played and the scores. photo this is the code for the one above: code

I tried getting the whole and it worked, the only thing i can't figure out is how to get the score and teams out of it.

pages url: https://www.fotmob.com/?date=20221118&q=

CodePudding user response:

First Identify the parent anchor tag and then iterate the parent element to find the specific child element. Scores are not available for all the matches since some of them have not started yet. Use try..except block in that case.

driver.get('https://www.fotmob.com/?date=20221118&q=')
elements=WebDriverWait(driver,10).until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, "div.show-webkit-scroll a[class*='MatchWrapper']")))
for element in elements:
    print(element.find_element(By.CSS_SELECTOR, "span[class*='TeamName']:nth-of-type(1)").text)
    try:
        element.find_element(By.CSS_SELECTOR, "span[class$='-score']")
        print(element.find_element(By.CSS_SELECTOR, "span[class$='-score']").text)
    except:
        print("No score record found")
    print(element.find_element(By.CSS_SELECTOR, "span[class*='TeamName']:nth-of-type(2)").text)
    print("===============================")

You need to import below libraries.

from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By

Output:

Cameroon
0 - 0
Panama
===============================
Belgium
No score record found
Egypt
===============================
Bahrain
No score record found
Serbia
===============================
Lommel
No score record found
Genk U23
===============================
Virton
No score record found
SL 16 FC
===============================
Rozova Dolina
No score record found
Ludogorets Razgrad
===============================
Nanjing City FC
0 - 5
Shanghai Port
===============================
Suzhou Dongwu
2 - 2
Wuhan Yangtze River
===============================
Dalian Professional FC
0 - 1
Zhejiang Professional
===============================
Slovacko
No score record found
Mlada Boleslav
===============================
Karvina
No score record found
Slavia Prague
===============================
Skive
No score record found
Esbjerg fB
===============================
Portsmouth
No score record found
Derby
===============================
Latvia U21
No score record found
Estonia U21
===============================
Spain U21
No score record found
Japan U21
===============================
Portugal U21
No score record found
Czech Republic U21
===============================
SC Weiche Flensburg
No score record found
Hamburger SV II
===============================
FCA Walldorf
No score record found
Bahlinger SC
===============================
Kickers Offenbach
No score record found
Eintracht Trier
===============================
TSG Balingen
No score record found
TuS RW Koblenz
===============================
Borussia Mönchengladbach II
No score record found
Schalke 04 II
===============================
Oberhausen
No score record found
FC Bocholt
===============================
Kalamata
No score record found
Panachaiki
===============================
Aizawl FC
0 - 1
Gokulam FC
===============================
Neroca FC
No score record found
Sudeva FC
===============================
East Bengal FC
No score record found
Odisha FC
===============================
Persik
No score record found
Persita
===============================
Persib Bandung
No score record found
Bhayangkara FC
===============================
Fajr Sepasi
0 - 0
Shahrdari Hamedan
===============================
Mes Shahr Babak
0 - 0
Khalij Fars Mahshahr
===============================
Saipa
0 - 0
Arman Gohar
===============================
Shahrdari Astara
0 - 0
Esteghlal Molasani
===============================
Shams Azar
0 - 0
Chooka
===============================
Van Pars Naqsh Jahan
No score record found
Omid Vahdat Khorasan
===============================
Esteghlal Kh
No score record found
Chadormalu Ardakan SC
===============================
Pars Jonoubi
No score record found
Darya Caspian Babol
===============================
FC Eindhoven
No score record found
Telstar
===============================
Helmond Sport
No score record found
De Graafschap
===============================
Ballymena United
No score record found
Linfield
===============================
Larne
No score record found
Dungannon Swifts
===============================
Lechia Gdansk
No score record found
Gornik Zabrze
===============================
B-SAD
No score record found
Boavista
===============================
Arouca
No score record found
Feirense
===============================
Albirex Niigata FC
No score record found
Balestier Khalsa FC
===============================
Granada
No score record found
Albacete
===============================
Xamax
No score record found
Wil
===============================
Bellinzona
No score record found
Thun
===============================
Yeni Malatyaspor
No score record found
Pendikspor
===============================
Zorya
No score record found
FC Olexandriya
===============================
Connah's Quay Nomads
No score record found
Caernarfon
===============================
Aberystwyth
No score record found
Pontypridd United
===============================
  • Related