Python/Crawling

쿠팡 _ 특정 상품 가격 크롤링(selenium)

haedal-uni 2021. 9. 9. 22:07
728x90

쿠팡에서 상품을 검색하면 가격이 뜨는데 그 가격이 매일 달라진다.

이를 이용해 가격이 낮을 때를 확인하기 위해 크롤링했다.

해당 크롤링은 상품을 검색하고 나오는 첫 페이지만을 크롤링했다.

 

 

 

selenium

처음에는 selenium으로 작성했었다.

가상선택자를 이용해서 css 선택자를 적었는데 실제로 실행해보면 적용이 되지 않았고

가상선택자를 이용해서 요소 검사를 해보면 실행이 되지 않았다.

 

 

label.item-name 요소 검사

 

 

가격이 낮을 때 상품을 구매하기 위해서 낮은 가격순으로 보게 한 다음 출력시키려고 했는데

css 선택자를 지정하는게 어려웠다.

 

그래서 대안으로 가격 범위를 지정해서 해당 상품만 크롤링하는 것으로 코드를 작성했는데

아래와 같은 화면이 계속 나오게 되었고

 

 

오류 화면

 

 

 

아래 코드를 사용하면 될 것이라는 글을 보아서 실행해 보았지만 여전히 같은 화면이 나와서 

selenium 대신 BeautifulSoup를 사용하게 되었다.

 

options = webdriver.ChromeOptions()
options.add_argument("user-agent=Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko")
browser = webdriver.Chrome("./chromedriver", options = options)

 

 

 

에러가 실행된다.

 

 

 

 

selenium 전체 코드

 

더보기
더보기

 

* 중간에 Access Denied 에러 화면이 떠서 그 이후로는 실행이 잘 되는지 여러 번 확인 불가능했다.

 

from selenium import webdriver
import time
import urllib.parse as par  # 한글을 특수한 문자로 변환시켜 주기 위해서
from selenium.webdriver import ActionChains
from selenium.webdriver.common.keys import Keys


options = webdriver.ChromeOptions()
options.add_argument("user-agent=Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko")
browser = webdriver.Chrome("./chromedriver", options = options)

keyword = "오그래놀라팝"
encoded = par.quote(keyword) # 한글 -> 특수한 문자


browser.get("https://www.coupang.com/np/search?rocketAll=false&q={}".format(encoded))
time.sleep(3)
print("쿠팡에서 오그래놀라팝을 검색 중입니다. ")

min = browser.find_element_by_css_selector("span > input.param-pricerange").click()
ActionChains(browser).send_keys('1').key_up('1').perform()
time.sleep(1)
ActionChains(browser).send_keys(Keys.TAB).key_up(Keys.TAB).perform()
time.sleep(1)
ActionChains(browser).send_keys('7000').perform()
time.sleep(1)
ActionChains(browser).send_keys(Keys.TAB).key_up(Keys.TAB).perform()
time.sleep(1)
browser.find_element_by_css_selector("a.btn-price-submit").click()

time.sleep(3)
product = browser.find_elements_by_css_selector("div.name")
price = browser.find_elements_by_css_selector("em.sale > .price-value")
time.sleep(3)
print("1 ~ 7000원 이내의 상품 검색 ")

for i in range(len(product)) :
    print(product[i].text)
    print(price[i].text)
    print()

 

 

 

 

이후 BeautifulSoup를 통해 수정할 예정이다.

 

728x90

'Python > Crawling' 카테고리의 다른 글

쿠팡 최저가 봇 - 작업 스케줄러  (0) 2022.01.04
bs4 와 selenium  (0) 2021.11.28
코로나 확진자 수 크롤링2 추가 설명  (0) 2021.09.05
코로나 확진자 수 크롤링2  (0) 2021.09.03
코로나 확진자 수 크롤링  (0) 2021.09.01