파이썬 코딩

2024.11.29. [파이썬] 네이버 쇼핑 닭가슴살 크롤링🐔🍗

37song 2025. 1. 15. 19:31

https://cafe.naver.com/startcodingofficial/1403

실전편 강의 숙제 !!
혼자 또 해내고서 신나했던.. ㅋㅋㅋ

▼ 결과물

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from bs4 import BeautifulSoup
import pandas as pd
import requests
import time
import pyperclip

result = []

url = 'https://search.shopping.naver.com/search/all?query=%EB%8B%AD%EA%B0%80%EC%8A%B4%EC%82%B4'

driver = webdriver.Chrome()
driver.get(url)

# 현재 페이지 높이를 추적하면서 반복 스크롤
last_height = driver.execute_script("return document.body.scrollHeight")

while True:
    # 페이지 끝으로 스크롤
    driver.execute_script("window.scrollTo(0, document.body.scrollHeight)")
        # 콘텐츠 로딩 대기
    time.sleep(1)
        # 새 페이지 높이 확인
    new_height = driver.execute_script("return document.body.scrollHeight")
    if new_height == last_height:
        break  # 더 이상 스크롤되지 않으면 종료
    last_height = new_height

html = driver.page_source
soup = BeautifulSoup(html, 'html.parser')

products = [product.text for product in soup.select('.product_item__MDtDF > div > div:nth-of-type(2) > div.product_title__Mmw2K > a')]
links = [link.attrs['href'] for link in soup.select('.product_item__MDtDF > div > div:nth-of-type(2) > div.product_title__Mmw2K > a')]
prices = [int(price.text.strip().replace(',','')) for price in soup.select('.product_item__MDtDF > div > div:nth-of-type(2) > div:nth-of-type(2) > strong > .price > span.price_num__S2p_v > em')]

for i in range(0, 40):
    result.append([products[i], links[i],  prices[i]])

df = pd.DataFrame(result, columns=['상품명', '링크', '가격'])

df.to_excel('naver_ggoggo.xlsx')