1차 프로젝트인 컨버스(https://www.converse.co.kr) 사이트 테스트 크롤링 작업
우선 컨버스 온라인스토어의 상품 카테고리는 크게 신발, 의류, 아동으로 구성되어 있다. 그 중 신발 카테고리의 전체 상품을 프로젝트 시작 전 시험삼아 크롤링 하려고 한다.
우선 사이트 구조이다. 신발은 총 175개의 상품이 있는데 모든 신발들이 한 페이지에 로딩되는것이 아니라 스크롤을 내리면 일정한 개수의 신발이 추가적으로 로딩되는 구조였다. 따라서 모든 신발들의 정보가 담긴 HTML을 얻으려면 셀레니움을 사용해 스크롤을 끝까지 내려야 한다.
우선 전체 코드다.
def converse_shoes_crawl():
SCROLL_PAUSE_TIME = 3
URL = "https://www.converse.co.kr/category/shoes"
driver = webdriver.Chrome('/home/sungjunjin/chromedriver')
driver.get(URL)
last_height = driver.execute_script("return document.body.scrollHeight;")
new_height = 0
while True:
for i in range(11):
driver.execute_script(f"window.scrollTo({new_height}, document.body.scrollHeight * {i/10});")
time.sleep(0.01)
new_height = driver.execute_script("return document.body.scrollHeight;")
time.sleep(SCROLL_PAUSE_TIME)
if new_height == last_height:
break
last_height = new_height
SCROLL_PAUSE_TIME = 3
last_height = driver.execute_script("return document.body.scrollHeight;")
for i in range(11):
driver.execute_script(f"window.scrollTo({new_height}, document.body.scrollHeight * {i/10});")
time.sleep(0.01)
new_height = driver.execute_script("return document.body.scrollHeight;")
time.sleep(SCROLL_PAUSE_TIME)
if new_height == last_height:
break
last_height = new_height
실제로 new_height와 last_height 값을 출력해봤다.
new height : 3674
last height : 3674
new height : 5875
last height : 5875
new height : 8395
last height : 8395
new height : 10893
last height : 10893
new height : 13006
last height : 13006
new height : 15416
last height : 15416
new height : 17915
last height : 17915
new height : 19983
last height : 19983
new height : 21755
last height : 21755
new height : 21755 # break