I've been working on a program that automates the painful task of downloading each file from a website for courses one by one and sorting them using python. This is my first python webscraping project on this scale, as such I would love some advice and lessons on how to make my code better! Here is the website I'm scraping.
from selenium import webdriver
import time
import os
import shutil
import re
path = r'https://coursevania.courses.workers.dev/[coursevania.com]%20Udemy%20-%20Master%20the%20Coding%20Interview%20Data%20Structures%20+%20Algorithms/'
# For changing the download location for this browser temporarily
options = webdriver.ChromeOptions()
preferences = {"download.default_directory": r"E:\Utilities_and_Apps\Python\MY PROJECTS\Test data\Downloads", "safebrowsing.enabled": "false"}
options.add_experimental_option("prefs", preferences)
# Acquire the Course Link and Get all the directories
browser = webdriver.Chrome(chrome_options=options)
browser.get(r"https://coursevania.courses.workers.dev/[coursevania.com]%20Udemy%20-%20Master%20the%20Coding%20Interview%20Data%20Structures%20+%20Algorithms/")
time.sleep(2)
elements = browser.find_elements_by_css_selector(".mdui-text-truncate")
# loop for as many directories there are
for i in range(15, len(elements)):
# At each directory, it refreshes the page to update the webelements in the list, and returns the current directory that is being worked on
browser.get(path)
time.sleep(2)
elements = browser.find_elements_by_css_selector(".mdui-text-truncate")
element = elements[i]
# checks if the folder for the directory already exists
current_directory_name = element.text[11:].strip(" .")
current_folder_path = "E:\\Utilities_and_Apps\\Python\\MY PROJECTS\\Test data\Downloads\\" + current_directory_name
if os.path.exists(current_folder_path):
pass
else:
os.mkdir(current_folder_path)
# Formatting what has been downloaded and sorted, and
print(current_directory_name, "------------------------------", sep="\n")
# moves on to the directory to get the page with the files
element.click()
# pausing for a few secs for the page to load, and running the same mechanism to get each file using the same method used in directory
time.sleep(3)
files = browser.find_elements_by_css_selector(".mdui-text-truncate")
for j in range(len(files)):
files = browser.find_elements_by_css_selector(".mdui-text-truncate")
_file = files[j]
# constants for some if statements
download = True
move = True
current_file_name = _file.text[17:].strip()
# If file exists, then pass over it, and don't do anything, and moveon to next file
if os.path.exists(current_folder_path + "\\" + current_file_name):
pass
# If it doesnt exist, then depending on its extension, do specific actions with it
else:
# Downloads the mp4 files by clicking on it, and finding the input tag which contains the download link for vid in its value attribute
if ".mp4" in current_file_name:
_file.click()
time.sleep(2)
download_path = browser.find_element_by_css_selector("input").get_attribute("value")
current_file_name = re.search(r'https://coursevania.courses.workers.dev/\[coursevania.com\]%20Udemy%20-%20Master%20the%20Coding%20Interview%20Data%20Structures%20\+%20Algorithms/.+/(.+)', download_path, re.DOTALL).group(1)
# Checks if file exists again, incase the filename is different then the predicted filename orderly generated.
if os.path.exists(current_folder_path + "\\" + current_file_name):
move = False
download = False
# returns to the previous page with the files
browser.back()
# self explanatory
elif ".html" in current_file_name:
download_path = path + current_directory_name + "/" + current_file_name
if os.path.exists(current_folder_path + "\\" + current_file_name):
move = False
download = False
else:
# acquires the download location by going to the parent tag which is an a tag containing the link for html in its 'href' attribute
download_path = _file.find_element_by_xpath('..').get_attribute('href').replace(r"%5E", "^")
current_file_name = re.search(r'https://coursevania.courses.workers.dev/\[coursevania.com\]%20Udemy%20-%20Master%20the%20Coding%20Interview%20Data%20Structures%20\+%20Algorithms/.+/(.+)', download_path, re.DOTALL).group(1).replace("%20", " ")
time.sleep(2)
current_file_path = "E:\\Utilities_and_Apps\\Python\\MY PROJECTS\\Test data\Downloads\\" + current_file_name
# responsible for downloading it using a path, get allows downloading, by source links
if download:
browser.get(download_path)
# while the file doesn't exist/ it hasn't been downloaded yet, do nothing
while True:
if os.path.exists(current_file_path):
break
time.sleep(1)
# moves the file from the download spot to its own folder
if move:
shutil.move(current_file_path, current_folder_path + "\\" + current_file_name)
print(current_file_name)
# formatter
print("------------------------------", "", sep="\n")
time.sleep(3)
Output:
1. Introduction
------------------------------
1. How To Succeed In This Course.mp4
1. How To Succeed In This Course.vtt
1.1 Interview Mind Map.html
1.2 Technical Interview Mind Map.html
2. Join Our Online Classroom!.html
3. Exercise Meet The Community!.html
------------------------------
10. Data Structures Trees
------------------------------
1. Trees Introduction.mp4
1. Trees Introduction.vtt
1.1 Technical Interview Mind Map.html
10. Solution lookup().mp4
10. Solution lookup().vtt
10.1 Solution Code.html
11. Bonus Exercise remove().mp4
11. Bonus Exercise remove().vtt
11.1 Exercise Repl.html
11.2 Binary Search Tree VisuAlgo.html
12. Solution remove().mp4
12. Solution remove().vtt
12.1 Solution Code.html
13. AVL Trees + Red Black Trees.mp4
13. AVL Trees + Red Black Trees.vtt
14. Resources AVL Trees + Red Black Trees.html
15. Binary Heaps.mp4
15. Binary Heaps.vtt
15.1 VisuAlgo Binary Heap.html
16. Quick Note on Heaps.mp4
16. Quick Note on Heaps.vtt
16.1 A great explanation of why.html
17. Priority Queue.mp4
17. Priority Queue.vtt
17.1 Priority Queue Javascript Code.html
18. Trie.mp4
18. Trie.vtt
19. Tree Review.mp4
19. Tree Review.vtt
19.1 Technical Interview Mind Map.html
2. Binary Trees.mp4
2. Binary Trees.vtt
3. O(log n).mp4
3. O(log n).vtt
4. Correction Binary Search Trees.html
5. Binary Search Trees.mp4
5. Binary Search Trees.vtt
5.1 Binary Search Tree VisuAlgo.html
6. Balanced VS Unbalanced BST.mp4
6. Balanced VS Unbalanced BST.vtt
6.1 Big O Cheat Sheet.html
7. BST Pros and Cons.mp4
7. BST Pros and Cons.vtt
8. Exercise Binary Search Tree.mp4
8. Exercise Binary Search Tree.vtt
8.1 Exercise Repl.html
9. Solution insert().mp4
9. Solution insert().vtt
9.1 Solution Code.html
------------------------------
... To Be Continued