Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- from selenium import webdriver
- import os
- import sqlite3
- path_to_current_dir = os.getcwd()
- def prepareTable(db_cursor, db_connection):
- db_cursor.execute('''DROP TABLE IF EXISTS `mecze`;''') # usuniecie db jesli istniala
- db_cursor.execute('''CREATE TABLE `mecze` (
- `data` TEXT,
- `gospodarz` TEXT,
- `przeciwnik` TEXT,
- `wynik` TEXT,
- `liga` TEXT
- );''') # utworzenie nowej tabeli mecze
- db_connection.commit()
- def getResultsWithDBInsert(flashscore_url, zrodlo):
- """
- funkcja pobiera url z którego ma pobrac wyniki ze strony flashscore, drugim argumentem jest zrodlo (nazwa ligi do dodania dla DB)
- """
- driver.get(flashscore_url)
- elem = driver.find_elements_by_class_name("stage-finished")
- for tr in elem[:10]: # pobiera 10 ostatnich meczy
- try:
- date = tr.find_element_by_class_name("time").text
- team_home = tr.find_element_by_class_name("team-home").text
- team_away = tr.find_element_by_class_name("team-away").text
- score = tr.find_element_by_class_name("score").text
- curr.execute("INSERT INTO mecze VALUES ('{data}', '{gospodarz}', '{przeciwnik}', '{wynik}', '{liga}')".format(
- data=date,
- gospodarz=team_home,
- przeciwnik=team_away,
- wynik=score,
- liga=zrodlo)) # dodanie do DB
- if len(date) < 2 or len(score) <= 1:
- continue
- conn.commit() # zatwierdzenie zmian w DB
- except Exception as e:
- print("Problem with insert to DB or printing results. Error {e}".format(e=e))
- continue
- print("[" + date + "]", team_home, score, team_away)
- if __name__ == "__main__":
- conn = sqlite3.connect('mecze.db')
- curr = conn.cursor()
- prepareTable(curr, conn) # wywolanie funkcji tworzacej DB/table
- driver = webdriver.Chrome(path_to_current_dir + "/chromedriver")
- print("Premier League (ENG):")
- getResultsWithDBInsert("https://www.flashscore.pl/pilka-nozna/anglia/premier-league/wyniki/","Premier League")
- print()
- print("Lique 1 (FRA):")
- getResultsWithDBInsert("https://www.flashscore.pl/pilka-nozna/francja/ligue-1/wyniki", "Ligue 1")
- print()
- print("Bundesliga (GER)")
- getResultsWithDBInsert("https://www.flashscore.pl/pilka-nozna/niemcy/bundesliga/wyniki", "Bundesliga")
- print()
- print("LaLiga (ESP)")
- getResultsWithDBInsert("https://www.flashscore.pl/pilka-nozna/hiszpania/laliga/wyniki", "LaLiga")
- print()
- print("Serie A (ITA)")
- getResultsWithDBInsert("https://www.flashscore.pl/pilka-nozna/wlochy/serie-a/wyniki", "Serie A")
- driver.close()
- conn.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement