Created
May 1, 2020 04:05
-
-
Save SikandAlex/72f4891c534f98cdebd947ecb45aa915 to your computer and use it in GitHub Desktop.
Python/Selenium script for scraping the hex codes from the gradients on uigradients.com
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from selenium import webdriver | |
import time | |
import json | |
names = [] | |
driver = webdriver.Chrome('/Users/alex/Downloads/chromedriver') | |
driver.implicitly_wait(10) | |
# Get the page | |
driver.get('https://uigradients.com/#GradeGrey') | |
# Global dictionary of name: color | |
gradients = dict() | |
def hex_to_rgb(hex): | |
hex = hex.lstrip('#') | |
hlen = len(hex) | |
return tuple(int(hex[i:i + hlen // 3], 16) for i in range(0, hlen, hlen // 3)) | |
def addColor(): | |
# Get the colors | |
hexColors = driver.find_elements_by_class_name("hex__name") | |
# Get the name of the gradient | |
gradientName = driver.find_element_by_class_name("noselect").text.title() | |
# Colors for the current gradient | |
colors = [] | |
# Iterate over the colors | |
for c in hexColors: | |
rgb = hex_to_rgb(c.text) | |
rgb2 = (round(rgb[0] / 255, 2), round(rgb[1] / 255, 2), round(rgb[2] / 255, 2)) | |
swiftColor = "UIColor(red: {}, green: {}, blue: {}, alpha: 1.00)".format(rgb2[0], rgb2[1], rgb2[2]) | |
colors.append(swiftColor) | |
gradients[gradientName] = colors | |
gradientName = "Grade Grey" | |
while gradientName not in names: | |
addColor() | |
names.append(gradientName) | |
time.sleep(1.2) | |
nextButton = driver.find_element_by_class_name("nav__arrow--right") | |
nextButton.click() | |
gradientName = driver.find_element_by_class_name("noselect").text.title() | |
file = open("Gradients.swift", "w") | |
file.write(json.dumps(gradients)) | |
file.close() | |
driver.quit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment