Сначала необходимо найти основную таблицу, а затем все теги <div>
, содержащие строки данных. Затем вы можете l oop на элементы в строке, чтобы получить текстовые данные. Я добавил строку прогресса в l oop, надеюсь, вам понравится:)
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import os
import time
import sys
driver = webdriver.Chrome(executable_path =os.path.abspath(os.getcwd()) + "/chromedriver")
driver.get("https://www.flashscore.com/football/england/premier-league-2018-2019/results/")
# extend table
show_more_buttons = driver.find_elements_by_xpath("//a[@class='event__more event__more--static']")
while len(show_more_buttons) > 0:
show_more_buttons[0].send_keys(Keys.ENTER)
time.sleep(2)
show_more_buttons = driver.find_elements_by_xpath("//a[@class='event__more event__more--static']")
# get table and events
table = driver.find_element_by_xpath('//*[@id="live-table"]/div[1]/div/div')
events = table.find_elements_by_class_name('event__match.event__match--static.event__match--oneLine')
# loop over events and collect data
count = 1
data = []
for item in events:
time = item.find_element_by_class_name('event__time').text
participant_home = item.find_element_by_class_name('event__participant.event__participant--home').text
event_scores = item.find_element_by_class_name('event__scores.fontBold').text
participant_away = item.find_element_by_class_name('event__participant.event__participant--away').text
event_part = item.find_element_by_class_name('event__part').text
data.append([time, participant_home, event_scores.replace('\n', ''), participant_away, event_part])
sys.stdout.write('\r')
sys.stdout.write("progress: %.2f %%" % ((count/len(events))*100))
sys.stdout.flush()
count += 1
for item in data:
print(item)
Вывод:
['12.05. 16:00', 'Brighton', '1 - 4', 'Manchester City', '(1 - 2)']
['12.05. 16:00', 'Burnley', '1 - 3', 'Arsenal', '(0 - 0)']
..
..
..
['11.08. 16:00', 'Watford', '2 - 0', 'Brighton', '(1 - 0)']
['11.08. 13:30', 'Newcastle', '1 - 2', 'Tottenham', '(1 - 2)']
['10.08. 21:00', 'Manchester Utd', '2 - 1', 'Leicester', '(1 - 0)']