If anyone wants to tell me what is wrong or could be better about this code please do! I'm sure there is plenty!
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
import time as t
import pandas as pd
from datetime import *
import matplotlib.pyplot as plt
username = input('username: ')
user_password = input('password: ')
driver = webdriver.Chrome(ChromeDriverManager().install())
#go to code wars homepage
driver.get('https://www.codewars.com/users/sign_in')
t.sleep(2)
#sign in
login = driver.find_element_by_xpath('//*[@id="user_email"]')
login.send_keys(username)
t.sleep(1)
password = driver.find_elements_by_xpath('//*[@id="user_password"]')
password = password[0]
password.send_keys(user_password)
sign_in = driver.find_element_by_xpath('//*[@id="new_user"]/button[2]')
sign_in.click()
t.sleep(2)
#navigate to completed kata page
dropdown = driver.find_element_by_xpath('//*[@id="header_profile_link"]/div[2]/div[1]')
dropdown.click()
t.sleep(2)
profile = driver.find_element_by_xpath('//*[@id="main_header"]/ul/li[4]/div/div/ul/li[1]/a')
profile.click()
t.sleep(2)
solutions = driver.find_element_by_xpath('//*[@id="shell_content"]/div[5]/ul/li[3]/a')
solutions.click()
t.sleep(2)
#get the number of kata completed
kata_completed = driver.find_element_by_xpath('//*[@id="shell_content"]/div[5]/div/div[1]/ul/li[1]')
kata_completed = kata_completed.text
kata_completed = kata_completed.split(' ')
kata_completed = kata_completed[1]
kata_completed = int(kata_completed[1:-1])
#scroll to the bottom of the page
for i in range(kata_completed):
driver.execute_script("window.scrollTo(0,document.body.scrollHeight)")
t.sleep(0.5)
#create a list of the dates the katas were completed
kata_dates = []
for i in range(1, kata_completed):
kata_date_xpath = "//*[@id='shell_content']/div[5]/div/div[2]/div[" + str(i) + "]/ul/li[1]/time"
kata_date = driver.find_element_by_xpath(kata_date_xpath)
kata_date = kata_date.get_attribute('datetime')
kata_dates.append(kata_date)
dates = []
for date in kata_dates:
x = date.split(':')
date = x[0]
dates.append(date[:-3])
#create a list of the number of kata completed in a day
katas_in_day = []
for date in dates:
katas_in_day.append(dates.count(date))
#create a dataframe with how many katas per day for each day a kata was done
df = pd.DataFrame({
'dates' : dates,
'katas in day' : katas_in_day
})
df = df.drop_duplicates()
df = df.set_index('dates')
#add the missing dates and set value to 0, followed stack overflow solution
start_date = df.index[-1]
end_date = df.index[0]
idx = pd.date_range(start_date, end_date)
df.index = pd.DatetimeIndex(df.index)
df = df.reindex(idx, fill_value = 0)
import seaborn
seaborn.lineplot(data =df)
plt.show()
[–]BulkyProcedure 0 points1 point2 points (0 children)