Adobe의 color 조합 데이터를 저장하기 위해 selenium으로 crawling을 구현했다.
from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.common.by import By
from webdriver_manager.chrome import ChromeDriverManager
from bs4 import BeautifulSoup
import time
options = webdriver.ChromeOptions()
driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()), options=options)
driver.get(fr'https://color.adobe.com/ko/explore')
time.sleep(10)
쿠키 설정이나 로그인 관련해서 팝업 창이 뜨는 경우 임의로 time.sleep(10)을 넣어 필요한 버튼을 클릭하거나 초기 설정을 선택할 수 있다.
ctrl + shift + c를 눌러 개발자 도구를 열고 저장하고 싶은 개체의 XPATH를 copy한다.
필요에 따라 class나 기타 속성을 copy한다.
count = 0 # 저장하고 싶은 데이터 수
page = 1
while count < 100000:
time.sleep(2) # 로딩 시간 설정
soup = BeautifulSoup(driver.page_source, 'html.parser')
colors = soup.find_all('div', class_='Theme__theme___FNytP')
for color in colors:
rgb_list = []
swatch = color.find_all('div', class_='Swatch__swatch___Y5UYS')
for s in swatch:
style_attribute = s['style']
rgb_value = [int(x) for x in style_attribute.split('rgb(')[1].split(')')[0].split(',')]
rgb_list.append(rgb_value)
with open("/home/example.jsonl", 'a') as file:
data = {}
data["palette"] = rgb_list
json.dump(data, file)
file.write('\n')
count += 1
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
time.sleep(2)
button = driver.find_element(By.XPATH, '//*[@id="color-root"]/div/div[5]/button[3]')
button.click()
print(f"page: {page}, count: {count}")
page += 1
로딩이 오래 걸려 한 번에 데이터가 불러와지지 않는다면 window scroll를 수행하고 대기 시간을 넣어 해결할 수 있다.
저장된 형태는 아래와 같다.
{"palette": [[4, 41, 64], [0, 92, 83], [159, 193, 49], [219, 242, 39], [214, 213, 142]]}
{"palette": [[218, 253, 186], [154, 235, 163], [69, 196, 176], [19, 103, 138], [1, 32, 48]]}
{"palette": [[52, 136, 136], [34, 186, 187], [158, 248, 238], [250, 127, 8], [242, 68, 5]]}