커뮤니티
포인트
쿠폰
내 강의실
국비 신청 내역
증명서
계정
로그아웃
학습 질문
개발 일지
나의 활동
답변 완료
3-4. 네이버 쇼핑에서 일반 상품외에도 광고 및 슈퍼패스 구좌도 크롤링하고 싶은데 일반 구좌랑 동일하게 해도 오류가 발생합니다.
[왕초보] 칼퇴를 부르는 파이썬 업무 자동화
3주차
북마크
김*경
댓글
3
추천
0
조회수
8
조회수
8
답변 완료

* 겪고 있는 문제 상황을 최대한 자세하게 작성해주세요.

* 문제 해결을 위해 어떤 시도를 해보았는지 구체적으로 함께 알려주세요.


네이버 가격비교 페이지에서 광고 및 슈퍼패스 구좌도 가져오게 하려면 어떻게 해야 할까요? 일반 구좌랑 동일하게 해도 해당 화면에 검색되지 않는 이상한 값이 가져와짐. (특히나 광고 쪽)




스파르타 즉문즉답스파르타 즉문즉답



작성한 코드 및 에러 메세지

from selenium import webdriver

from selenium.webdriver.common.by import By

from selenium.webdriver.common.keys import Keys

from selenium.webdriver.chrome.options import Options

import time

from pprint import pprint


from openpyxl import load_workbook

from openpyxl.drawing.image import Image

import requests

from io import BytesIO


chrome_options = Options()

chrome_options.add_experimental_option("detach", True)

chrome_options.add_experimental_option("excludeSwitches", ["enable-logging"])

driver = webdriver.Chrome(options=chrome_options)


# 웹 사이트 열기

driver.get('https://www.naver.com')

# driver.get('https://search.naver.com/search.naver?where=nexearch&sm=top_hty&fbm=0&ie=utf8&query=%EA%B0%95%EC%95%84%EC%A7%80%EA%B0%84%EC%8B%9D')

time.sleep(2)

search = driver.find_element(By.CSS_SELECTOR, "#query")

search.click()


search.send_keys("강아지 간식")

search.send_keys(Keys.ENTER)


time.sleep(2)

driver.find_element(By.CSS_SELECTOR, "#main_pack > section.sc_new.sp_nshop._shopping_root._slog_visible > div:nth-child(1) > div.mod_more_wrap._more_root > a > span.kwd").click()

time.sleep(2)

new_window = driver.window_handles[1]

driver._switch_to.window(new_window)

driver.maximize_window()


before_h = driver.execute_script("return window.scrolly")

while True:

    driver.find_element(By.CSS_SELECTOR, "body").send_keys(Keys.END) #스크롤 내리기 위해 END 키 누르기

    time.sleep(2)

    after_h = driver.execute_script("return window.scorlly") #이동하고 나서 스크롤 위치

    if after_h == before_h: # 만약에 옮기기 전화 후 의 스크롤 위치가 동일하면(=끝까지 내렸다면)

        break

    before_h = after_h



def get_item(tag, items):

    for item in items:

        name = ''

        if tag == 1:

            name = '#content > div.style_content__xWg5l > div.basicList_list_basis__uNBZx > div > div:nth-child(2) > div > div.adProduct_info_area__dTSZf > div.adProduct_title__amInq > a'

        elif tag == 2:

            name = '#content > div.style_content__xWg5l > div.basicList_list_basis__uNBZx > div > div.superSavingProduct_item__ziVFy > div > div.superSavingProduct_info_area__qriC2 > div.superSavingProduct_title__1W3DY'

        elif tag == 3:

            name = '.product_title__Mmw2K'


        #상품명

        names = item.find_elements(By.CSS_SELECTOR, name)

        for i in names:

            print("111111", i)

        name = names[0].text if names else "이름없음"


        #가격찾기

        prices = item.find_elements(By.CSS_SELECTOR, 'div.product_price_area__eTg7I > strong > span.price > span.price_num__S2p_v')

        price = prices[0].text if prices else "가격없음"


        #링크찾기

        links = item.find_elements(By.CSS_SELECTOR, '.product_title__Mmw2K >  a')

        link = links[0].text if links else "링크없음"


        #리뷰개수

        reviews_totals = item.find_elements(By.CSS_SELECTOR, 'div.product_etc_box__ElfVA > a:nth-child(1) > em.product_num__fafe5')

        review_total = reviews_totals[0].text if reviews_totals else "리뷰 없음"


        #평점

        scores = item.find_elements(By.CSS_SELECTOR, '.product_grade__IzyU3')

        score = scores[0].text.split('\n')[1] if scores else "평점 없음"


        #구매횟수

        purchases = item.find_elements(By.CSS_SELECTOR, 'div.product_etc_box__ElfVA > a:nth-child(2) > span > span > em.product_num__fafe5')

        purchase = purchases[0].text if purchases else "알수없음"


        #찜

        likes = item.find_elements(By.CSS_SELECTOR, 'span.product_num__fafe5')

        like = likes[0].text if likes else "찜 없음"


        #등록일

        open_dates = item.find_elements(By.CSS_SELECTOR, '.product_etc__LGVaW')

        open_date = open_dates[0].text if open_dates else "리뷰 없음"


        #이미지

        images = item.find_elements(By.CSS_SELECTOR, '.thumbnail_thumb__Bxb6Z  > img')

        image = images[0]. get_attribute('src') if images else "이미지 없음"


        product_info = {

            'name:': name,

            'price:': price,

            'link:': link,

            'review_total:': review_total,

            'score:': score,

            'purchase:': purchase,

            'like:': like,

            'open_date:': open_date,

        }


        pprint(product_info)


print("-" * 100)

print("광고")

items_mkt = driver.find_elements(By.CSS_SELECTOR, ".adProduct_item__1zC9h")

get_item(1, items_mkt)



print("-" * 100)

print("슈퍼적립")

items_super = driver.find_elements(By.CSS_SELECTOR, ".superSavingProduct_item__ziVFy")

get_item(2, items_super)



# print("-" * 100)

# print("일반")

# items = driver.find_elements(By.CSS_SELECTOR, ".product_item__MDtDF")

# get_item(3, items)



# driver.quit()


스파르타 즉문즉답


취소
 공유
취소
댓글 0
댓글 알림
나의얼굴