5.6 extracting_data_to_a_csv_file_with_pandas.py
Wed Jan 31 2024 19:50:21 GMT+0000 (Coordinated Universal Time)
Saved by
@mnis00014
#python
#selenium
#scraping
# --------------------------- Chrome ---------------------------------
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.by import By
import pandas as pd
import time
path = r"C:\Drivers\chromedriver-win64\chromedriver.exe"
website = "https://www.adamchoi.co.uk/overs/detailed"
# Use the Service class to specify the path to chromedriver.exe
service = Service(executable_path=path)
# Use ChromeOptions for additional configurations
options = webdriver.ChromeOptions()
options.add_experimental_option("detach", True)
# Initialize the WebDriver with the specified service and options
driver = webdriver.Chrome(service=service, options=options)
# Navigate to the specified website
driver.get(website)
all_matches_button = driver.find_element("xpath", '//label[@analytics-event="All matches"]')
all_matches_button.click()
dropdown = Select(driver.find_element(By.ID, "country"))
dropdown.select_by_visible_text('Spain')
time.sleep(3)
matches = driver.find_elements(By.TAG_NAME, "tr")
date = []
home_team = []
score = []
away_team = []
for match in matches:
date.append(match.find_element("xpath", "./td[1]").text)
home_team.append(match.find_element("xpath", "./td[2]").text)
score.append(match.find_element("xpath", "./td[3]").text)
away_team.append(match.find_element("xpath", "./td[4]").text)
# Close the WebDriver when you're done
driver.quit()
df = pd.DataFrame({'date': date,
'home_team': home_team,
'score': score,
'away_team': away_team})
df.to_csv('football_data.csv', index=False)
print(df)
content_copyCOPY
Comments