-
쇼핑몰 상품 2가지 옵션 조합 크롤링하기코딩 연습/파이썬 크롤링 2022. 6. 15. 19:26
[네이버 지식인 질문 내용 답변] 쇼핑몰에서 2가지 옵션인 색상(블랙, 핑크)과 사이즈(블랙: S, M, L 핑크: S, M)가 있고 색상을 선택하면 그에 따라 사이즈가 동적으로 바뀌는 경우, 색상과 사이즈의 조합을 추출하는 코드임
쇼핑몰 2가지 옵션에 대한 조합 크롤링
쇼핑몰 개발자도구 [파이썬 소스 코드]
from selenium import webdriver from selenium.webdriver.chrome.service import Service as ChromeService from selenium.webdriver.chrome.options import Options from webdriver_manager.chrome import ChromeDriverManager from selenium.webdriver.common.by import By import time chrome_service = ChromeService(executable_path=ChromeDriverManager().install()) options = Options() options.add_experimental_option('excludeSwitches', ['enable-logging']) user_agent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.75 Safari/537.36' options.add_argument(f'user-agent={user_agent}') options.add_argument('--headless') driver = webdriver.Chrome(service=chrome_service, options=options) driver.get("https://withstory.co.kr/product/detail.html?product_no=524&cate_no=27&display_group=1") driver.implicitly_wait(2) # 웹 페이지(html) 요소 찾기 및 가져오기 select_box = driver.find_elements(By.CSS_SELECTOR, "#product_option_id1 > option") for i, option in enumerate(select_box): if i > 1: option.click() print(option.text) time.sleep(1) select_box2 = driver.find_elements(By.CSS_SELECTOR, "#product_option_id2 > option") for i, option in enumerate(select_box2): if i > 1: print(option.text) driver.quit()
- 동적으로 움직이는 select box에 대해 selenium과 크롬 웹 드라이버를 활용하여 추출함
- option 부분에서 index 0과 1은 추출할 사이즈 데이터가 아니기 때문에 if 조건문으로 제외시킴
[실행 결과]
====== WebDriver manager ====== Current google-chrome version is 102.0.5005 Get LATEST chromedriver version for 102.0.5005 google-chrome Driver [C:\Users\hxxxx\.wdm\drivers\chromedriver\win32\102.0.5005.61\chromedriver.exe] found in cache 블랙 S M L 핑크 S M
'코딩 연습 > 파이썬 크롤링' 카테고리의 다른 글
네이버 검색 키워드 자동완성어 추출 (0) 2022.06.29 파이썬 크롤링 기초 예제 (0) 2022.06.16 네이버 블로그의 전체 페이지 저장하기(방법 설명) (5) 2022.06.13 문자로 된 티스토리 블로그 주소(포스트 주소) 파이썬 크롤링 방법 (0) 2022.06.09 스마트 스토어 데이터 크롤링하여 sqlite3에 저장하기 (0) 2022.06.02