• 카테고리

    질문 & 답변
  • 세부 분야

    데이터 분석

  • 해결 여부

    미해결

네이버쇼핑 질문 있습니다!

24.07.01 23:22 작성 조회수 57

0

네이버쇼핑크롤링을 연습도중 상품썸네일 링크를 가져와보고싶어서 연습을해봤는데 아래링크처럼했을때 첫번째 상품의 이미지링크만 가져오게되고 나머지 이미지썸네일 링크는 여러가지 다른선택자를 이용해서 시도를 해봤지만 이와같이 한개의 이미지썸네일 링크만 가져와져서 이럴때 어떻게 해야될지 궁금합니다!

image.png

from selenium import webdriver
import time
from bs4 import BeautifulSoup
import re
from urllib.parse import urlparse, urlunparse

driver = webdriver.Chrome()

driver.get('https://search.shopping.naver.com/search/all?adQuery=%EB%8B%AD%ED%84%B8%EB%BD%91%EB%8A%94%EA%B8%B0%EA%B3%84&origQuery=%EB%8B%AD%ED%84%B8%EB%BD%91%EB%8A%94%EA%B8%B0%EA%B3%84&pagingIndex=1&pagingSize=40&productSet=overseas&query=%EB%8B%AD%ED%84%B8%EB%BD%91%EB%8A%94%EA%B8%B0%EA%B3%84&sort=review&sps=N&timestamp=&viewType=list')

last_height = driver.execute_script("return document.body.scrollheight")

while True:
    #스크롤끝까지 내리기
    driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
    time.sleep(2)
    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') 

items = soup.select(".product_item__MDtDF")
for item in items:
    # 이미지 URL 찾기
    img = item.select_one(".product_img_area__cUrko img")
    print(img)

답변 3

·

답변을 작성해보세요.

0

이미지의 경우 한번에 확확 내리면,

로딩이 안되는 문제가 있을 수 있습니다.

 

아래 처럼 500px 씩 내리면 잘 가져와 집니다.

from selenium import webdriver
import time
from bs4 import BeautifulSoup

driver = webdriver.Chrome()

driver.get('https://search.shopping.naver.com/search/all?adQuery=%EB%8B%AD%ED%84%B8%EB%BD%91%EB%8A%94%EA%B8%B0%EA%B3%84&origQuery=%EB%8B%AD%ED%84%B8%EB%BD%91%EB%8A%94%EA%B8%B0%EA%B3%84&pagingIndex=1&pagingSize=40&productSet=overseas&query=%EB%8B%AD%ED%84%B8%EB%BD%91%EB%8A%94%EA%B8%B0%EA%B3%84&sort=review&sps=N&timestamp=&viewType=list')

last_height = driver.execute_script("return document.body.scrollHeight")

scroll_increment = 500
current_position = 0

while True:
    # 스크롤을 500px씩 내리기
    driver.execute_script(f"window.scrollBy(0, {scroll_increment});")
    time.sleep(1)
    new_position = driver.execute_script("return window.pageYOffset + window.innerHeight")
    
    if new_position >= last_height:
        break
    
    current_position += scroll_increment
    last_height = driver.execute_script("return document.body.scrollHeight")

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

items = soup.select(".product_item__MDtDF")
for item in items:
    # 이미지 URL 찾기
    img = item.select_one(".product_img_area__cUrko img")
    print(img.attrs['src'])
https://shopping-phinf.pstatic.net/main_8617681/86176813957.1.jpg?type=f140
https://shopping-phinf.pstatic.net/main_4880094/48800941696.jpg?type=f140
https://shopping-phinf.pstatic.net/main_4779910/47799105369.1.jpg?type=f140
https://shopping-phinf.pstatic.net/main_8428246/84282460704.2.jpg?type=f140
https://shopping-phinf.pstatic.net/main_4463964/44639646195.3.jpg?type=f140
https://shopping-phinf.pstatic.net/main_4890732/48907326327.jpg?type=f140
https://shopping-phinf.pstatic.net/main_4851354/48513545508.1.jpg?type=f140
https://shopping-phinf.pstatic.net/main_4883898/48838989714.jpg?type=f140
https://shopping-phinf.pstatic.net/main_4884817/48848173219.jpg?type=f140
https://shopping-phinf.pstatic.net/main_3604600/36046007226.11.jpg?type=f140
https://shopping-phinf.pstatic.net/main_4761499/47614998745.1.jpg?type=f140
https://shopping-phinf.pstatic.net/main_3604508/36045081526.9.jpg?type=f140
https://shopping-phinf.pstatic.net/main_4050448/40504485967.1.jpg?type=f140
https://shopping-phinf.pstatic.net/main_4034323/40343232242.2.jpg?type=f140
https://shopping-phinf.pstatic.net/main_8682114/86821148412.jpg?type=f140
https://shopping-phinf.pstatic.net/main_4881658/48816584813.jpg?type=f140
https://shopping-phinf.pstatic.net/main_3954638/39546389455.9.jpg?type=f140
https://shopping-phinf.pstatic.net/main_4526615/45266156072.1.jpg?type=f140
https://shopping-phinf.pstatic.net/main_4318257/43182578358.3.jpg?type=f140
https://shopping-phinf.pstatic.net/main_4761465/47614656834.jpg?type=f140
https://shopping-phinf.pstatic.net/main_4749386/47493863539.jpg?type=f140
https://shopping-phinf.pstatic.net/main_3293110/32931102622.2.jpg?type=f140
https://shopping-phinf.pstatic.net/main_4783335/47833355007.jpg?type=f140
https://shopping-phinf.pstatic.net/main_3958392/39583929097.2.jpg?type=f140
https://shopping-phinf.pstatic.net/main_4451192/44511926827.1.jpg?type=f140
https://shopping-phinf.pstatic.net/main_4759973/47599735512.jpg?type=f140
https://shopping-phinf.pstatic.net/main_8462270/84622704297.jpg?type=f140
https://shopping-phinf.pstatic.net/main_4332941/43329417493.jpg?type=f140
https://shopping-phinf.pstatic.net/main_8774467/87744674433.jpg?type=f140
https://shopping-phinf.pstatic.net/main_8757149/87571495703.1.jpg?type=f140
https://shopping-phinf.pstatic.net/main_4881708/48817084668.jpg?type=f140
https://shopping-phinf.pstatic.net/main_4061272/40612727543.19.jpg?type=f140
https://shopping-phinf.pstatic.net/main_3604599/36045992083.9.jpg?type=f140
https://shopping-phinf.pstatic.net/main_3604590/36045906708.12.jpg?type=f140
https://shopping-phinf.pstatic.net/main_4885674/48856747878.jpg?type=f140
https://shopping-phinf.pstatic.net/main_4320170/43201702179.2.jpg?type=f140
https://shopping-phinf.pstatic.net/main_4644171/46441715344.1.jpg?type=f140
https://shopping-phinf.pstatic.net/main_8382614/83826144294.1.jpg?type=f140
https://shopping-phinf.pstatic.net/main_4884076/48840769905.jpg?type=f140
https://shopping-phinf.pstatic.net/main_3875365/38753651095.jpg?type=f140
로인스님의 프로필

로인스

질문자

2024.07.05

wow!! 됬습니다 쌤 감사합니다 !

0

로인스님의 프로필

로인스

질문자

2024.07.04

이미 그렇게도 진행해봤는데 그렇게해도 40개중에 2개의 썸네일 링크만 가져왔습니다 ㅠㅠ 몇일동안 gpt 4o로 분석하면서 계속해봤는데도 동일한현상 이였습니다

40 (총40개상품중 아래 2개 링크만 가져옴 )

https://shopping-phinf.pstatic.net/main_8617681/86176813957.1.jpg?type=f140

https://shopping-phinf.pstatic.net/main_4880094/48800941696.jpg?type=f140

items = soup.select(".product_item__MDtDF")
print(len(items))
for item in items:
    # 이미지 URL 찾기
    img = item.select_one(".product_img_area__cUrko img")
    if img:
        # src 속성 확인
        img_src = img.get('src')
        # data-src 속성 확인
        img_data_src = img.get('data-src')
        # 이미지 URL 출력
        print(img_src if img_src else img_data_src)

0

인프런 AI 인턴님의 프로필

인프런 AI 인턴

2024.07.03

안녕하세요, 인프런 AI 인턴입니다.

네이버 쇼핑 크롤링을 하다보면, 동일한 클래스명에도 불구하고 실제로 웹 페이지에서 사용하는 태그 구조가 다르게 적용되어 있을 수 있습니다. 이러한 경우, 첫 번째 상품의 이미지 링크는 성공적으로 가져올 수 있지만, 나머지 상품의 이미지 링크를 가져오지 못하는 현상이 발생할 수 있습니다.

첫 번째로, 이미지 링크를 가져오는 부분에서 .product_img_area__cUrko img 선택자를 사용하고 계시는데, 여기서 img 태그가 실제 페이지에 어떻게 적용되어 있는지 확인해보아야 합니다. 일반적으로 img 태그의 src 속성이나 data-src 속성에 이미지의 URL이 포함되어 있습니다.

다음은 src 속성이 있는 경우와 data-src 속성이 있는 경우 이미지 링크를 가져오는 예시 코드입니다.

for item in items:
    # 이미지 URL 찾기
    img = item.select_one(".product_img_area__cUrko img")
    if img:
        # src 속성 확인
        img_src = img.get('src')
        # data-src 속성 확인
        img_data_src = img.get('data-src')
        # 이미지 URL 출력
        print(img_src if img_src else img_data_src)

위 코드에서는 img 태그에서 src 속성이 존재하면 그 값을 사용하고, 없을 경우 data-src 속성 값을 사용하여 이미지 링크를 가져옵니다.

여러 상품의 이미지 링크를 가져오기 위해선, 각 상품에 적용된 HTML 구조를 파악하고, 상품별로 적절한 선택자를 사용하여 이미지의 URL을 추출해야 합니다. 웹 페이지의 구조가 변경될 수 있으므로, 주기적으로 코드를 확인하고 업데이트하는 것이 중요합니다.

채널톡 아이콘