-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path08_selenium_scraping.py
More file actions
55 lines (37 loc) · 1.34 KB
/
Copy path08_selenium_scraping.py
File metadata and controls
55 lines (37 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
driver = webdriver.Safari()
url = "https://www.adamchoi.co.uk/overs/detailed"
driver.get(url)
# Find the element using xpath
all_matches_button = driver.find_element(By.XPATH, '//*[@id="page-wrapper"]/div/home-away-selector/div/div/div/div/label[2]')
all_matches_button.click()
matches = driver.find_elements(By.TAG_NAME, 'tr')
date = []
home_team = []
score = []
away_team = []
# X Path indexing starts with 1, Unlike python's 0
for i in range(5):
date.append(matches[i].find_element(By.XPATH, './td[1]').text)
home_team.append((matches[i].find_element(By.XPATH, './td[2]').text))
score.append((matches[i].find_element(By.XPATH, './td[3]').text))
away_team.append((matches[i].find_element(By.XPATH, './td[4]').text))
# Printing for checking process and debugging
print(home_team[i])
import pandas as pd
# Create a dictionary with the variables
data = {
'Date': date,
'Home Team': home_team,
'Score': score,
'Away Team': away_team
}
# Create a dataframe
df = pd.DataFrame(data)
# Export Data as CSV
df.to_csv('england_football_matches.csv', index=False)