Advertisement
Trawek

Flashscore

Jan 24th, 2019
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.89 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3.  
  4. from selenium import webdriver
  5.  
  6. import os
  7. import sqlite3
  8.  
  9. path_to_current_dir = os.getcwd()
  10.  
  11.  
  12. def prepareTable(db_cursor, db_connection):
  13.     db_cursor.execute('''DROP TABLE IF EXISTS `mecze`;''')  # usuniecie db jesli istniala
  14.     db_cursor.execute('''CREATE TABLE `mecze` (
  15.                         `data`  TEXT,
  16.                         `gospodarz` TEXT,
  17.                         `przeciwnik`    TEXT,
  18.                         `wynik` TEXT,
  19.                         `liga` TEXT
  20.                         );''') # utworzenie nowej tabeli mecze
  21.     db_connection.commit()
  22.  
  23.  
  24. def getResultsWithDBInsert(flashscore_url, zrodlo):
  25.     """
  26.        funkcja pobiera url z którego ma pobrac wyniki ze strony flashscore, drugim argumentem jest zrodlo (nazwa ligi do dodania dla DB)
  27.    """
  28.  
  29.     driver.get(flashscore_url)
  30.     elem = driver.find_elements_by_class_name("stage-finished")
  31.     for tr in elem[:10]:            # pobiera 10 ostatnich meczy
  32.         try:
  33.             date = tr.find_element_by_class_name("time").text
  34.             team_home = tr.find_element_by_class_name("team-home").text
  35.             team_away = tr.find_element_by_class_name("team-away").text
  36.             score = tr.find_element_by_class_name("score").text
  37.             curr.execute("INSERT INTO mecze VALUES ('{data}', '{gospodarz}', '{przeciwnik}', '{wynik}', '{liga}')".format(
  38.                 data=date,
  39.                 gospodarz=team_home,
  40.                 przeciwnik=team_away,
  41.                 wynik=score,
  42.                 liga=zrodlo)) # dodanie do DB
  43.  
  44.             if len(date) < 2 or len(score) <= 1:
  45.                 continue
  46.             conn.commit()   # zatwierdzenie zmian w DB
  47.  
  48.         except Exception as e:
  49.             print("Problem with insert to DB or printing results. Error {e}".format(e=e))
  50.             continue
  51.         print("[" + date + "]", team_home, score, team_away)
  52.  
  53.  
  54.  
  55. if __name__ == "__main__":
  56.     conn = sqlite3.connect('mecze.db')
  57.     curr = conn.cursor()
  58.  
  59.     prepareTable(curr, conn) # wywolanie funkcji tworzacej DB/table
  60.  
  61.     driver = webdriver.Chrome(path_to_current_dir + "/chromedriver")
  62.  
  63.     print("Premier League (ENG):")
  64.     getResultsWithDBInsert("https://www.flashscore.pl/pilka-nozna/anglia/premier-league/wyniki/","Premier League")
  65.     print()
  66.  
  67.     print("Lique 1 (FRA):")
  68.     getResultsWithDBInsert("https://www.flashscore.pl/pilka-nozna/francja/ligue-1/wyniki", "Ligue 1")
  69.     print()
  70.  
  71.     print("Bundesliga (GER)")
  72.     getResultsWithDBInsert("https://www.flashscore.pl/pilka-nozna/niemcy/bundesliga/wyniki", "Bundesliga")
  73.     print()
  74.  
  75.     print("LaLiga (ESP)")
  76.     getResultsWithDBInsert("https://www.flashscore.pl/pilka-nozna/hiszpania/laliga/wyniki", "LaLiga")
  77.     print()
  78.  
  79.     print("Serie A (ITA)")
  80.     getResultsWithDBInsert("https://www.flashscore.pl/pilka-nozna/wlochy/serie-a/wyniki", "Serie A")
  81.  
  82.     driver.close()
  83.     conn.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement