lundi 6 août 2018

Selenium/Python - "If not" statement followed by navigator.find_element

Good evening everyone,

I am currently trying to extract data from this website :

https://classic.sportsbookreview.com/betting-odds/nba-basketball/

The program logic is based on the followed loop.

Once the page from the website is open, the first step is to open the calendar and to seek if there were matches played this month.

If this is the case, for each of these day, the data will be written in an xls file. Then, when there aren't anymore matches to extract, the program click to the previous month, and perform the same statements.

On the contrary, if there isn't a single day where a match was played during the month, the treatment will click on the previous month and will perform the same statements as before.

Here is my code (sorry for the use of the french, if this is not clear, I could translate it) :

    from selenium import webdriver
    from selenium.webdriver.common.keys import Keys
    import time
    import xlsxwriter

    ## Création du fichier excel
    tableur = xlsxwriter.Workbook("PrédictionMachine.xlsx") ;
    feuille1 = tableur.add_worksheet("10-05-2018") ;

    ## Ouverture du navigateur (pour Chrome, remplacer Firefox par Chrome)
    navigateur = webdriver.Firefox() ;
    navigateur.get("https://classic.sportsbookreview.com/betting-odds/nba-        basketball/") ;  
    time.sleep(3) ;

    ## Initialisation des variables nécessaires pour écriture dans tableur
    row = 0 ;
    col = 0 ;
    moistraite = 0 ;
    matchtreeltrouve = 0 ;

    ## PHASE TEST À SUPPRIMER EN VF
    passage = 0

    try :
    ## Boucle : le programme s'arrête lorsque qu'une touche quelqconque du   clavier est pressée
       while True :

     ## Ouverture du calendrier
          print("Passage : ", passage) 
          Calendrier = navigateur.find_element_by_xpath("//a[@class='dd-go-button']").click() 
          time.sleep(3) 

     ## Récupération du mois/année en cours de traitement
          Mois = navigateur.find_element_by_xpath("//h2[@class='tbl-cal-top-middle']")

    ## Si aucun match n'est joué ce mois-ci, le programme clique sur le  mois précédent
          if not (navigateur.find_elements_by_xpath("//a[contains(@onclick, '20')]")) :
             print("------------------------------------------------") 
             print("Aucun match trouvé pour le mois de :", Mois.text) 
             print("------------------------------------------------") 
             Moisprecedent =  navigateur.find_element_by_xpath("//img[@alt='Left Arrow']").click() 
             moistraite += 1 
             print("------------------------------------------------") 
             print("Nombre de mois traité :", moistraite) ;
             print("------------------------------------------------")    
             time.sleep(3) ;
          else :

    ## Récupération de tous les jours du mois où un match a supposément été joué 
             JMtheorique = navigateur.find_elements_by_xpath("//a[contains(@onclick, '20')]")
             print("------------------------------------------------") 
             print("Matchs supposés trouvés pour le mois de :", Mois.text) 
             print("------------------------------------------------") 
             for jmtheorique in JMtheorique :
                 print("ON Y CROIT", jmtheorique.text)
                                                                               navigateur.find_element_by_xpath("//a[contains(@onclick, 'OddsEvent.GetLinkDate')]").click()
                 time.sleep(3)
          passage += 1
          Calendrier = navigateur.find_element_by_xpath("//a[@class='dd-go-button']").click() 
          time.sleep(2)
          Moisprecedent =   navigateur.find_element_by_xpath("//img[@alt='Left Arrow']").click() 
          moistraite += 1
    ## Fermeture du fichier et du navigateur
          print("******************************************") 
          print("Nombre de match écrit ", matchtreeltrouve) 
          print("******************************************") 
          tableur.close() ;
    ## Arrêt Manuel du programme
    except KeyboardInterrupt :
       print("!/_\/_\/_\! Interruption manuelle du programme !/_\/_\/_\! ") 
       print("Le programme a été stoppé au mois de :", Mois.text) 
       print("Nombre de mois traité :", moistraite) 
       print("Nombre de match écrit :", matchtreeltrouve) ;
    ##navigateur.quit()

For now, I just want to navigate through the different months/days. For August, the code source works fine.

But when I arrive at July, the program is looping. It seems that the 'if not' statement isnt' correctly written, since the treatment find a match whereas there isn't a single one played during this month.

Aucun commentaire:

Enregistrer un commentaire