• 카테고리

    질문 & 답변
  • 세부 분야

    데이터 분석

  • 해결 여부

    미해결

첫번째페이지크롤링 질문~!!!!

22.12.21 15:27 작성 22.12.21 15:40 수정 조회수 523

1

import requests
from bs4 import BeautifulSoup

main_url = "https://www.coupang.com/np/search?component=&q=%EA%B2%8C%EC%9D%B4%EB%B0%8D+%EB%A7%88%EC%9A%B0%EC%8A%A4&channel=auto"

# 헤더에 User-Agent, Accept-Language 를 추가하지 않으면 멈춥니다
header = {
    'Host': 'www.coupang.com',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:76.0) Gecko/20100101 Firefox/76.0',
    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
    'Accept-Language': 'ko-KR,ko;q=0.8,en-US;q=0.5,en;q=0.3',
}

response = requests.get(main_url, headers=header)
html = response.text
soup = BeautifulSoup(html, 'html.parser')

links = soup.select("a.search-product-link") # select의 결과는 리스트 자료형
for link in links:
    sub_url = "https://www.coupang.com" + link.attrs['href']

    response = requests.get(sub_url, headers=header)
    html = response.text
    soup = BeautifulSoup(html, 'html.parser')

    #try:
        #brand_name = soup.select_one("a.prod-brand-name").text
    #except:
        #brand_name = ""
    
    # 브랜드명
    #try:
        #brand_name = soup.select_one("a.prod-brand-name").text
    #except:
        #brand_name = ""
    #print(brand_name)
    
    # 상품명
    #product_name = soup.select_one("h2.prod-buy-header__title").text
    #product_name = ""

    # 가격
    #product_price = soup.select_one("span.total-price > strong").text
    #product_price = ""

    #print(brand_name, product_name, product_price)

 

for 문 안에 soup 를 print로 찍어보니

 <html lang="en"> <head> <script type="text/javascript"> document.location.replace("https://www.coupang.com/"); </script> </head> <body></body> </html>

<!DOCTYPE html>

<html lang="en"> <head> <script type="text/javascript"> document.location.replace("https://www.coupang.com/"); </script> </head> <body></body> </html>

 

이렇게 나와서 아래의 soup.select_one 값이 공백으로 나오네요.

brand_name = soup.select_one("a.prod-brand-name").text

 

강의랑 코드를 몇번씩 계속 돌려보는데도 이해가 안가네요

답변 1

답변을 작성해보세요.

0

image

안녕하세요!

제가 직접 수강생님의 코드를 복사해서

주석만 풀고 테스트해본 결과

브랜드명을 잘 가져오는 것으로 확인되었습니다.

 

코드 상에는 문제가 없는 것으로 판단되는데

재부팅 및 쿠키, 캐시 삭제 후 다시 실행해 보시겠어요?

 

wogkeoqkr님의 프로필

wogkeoqkr

질문자

2022.12.22

제 PC 에서는 왜 안될까요..??

계속 데이터를 불러오질 못하는데

버젼도 최신버전확인 했는데