[Python] 나도코딩 웹 스크래핑편 - (8) 쿠팡 노트북2

Updated:

유튜브 나도코딩 웹 스크래핑 무료 강의를 통해 학습한 내용을 정리해서 올리고 있습니다.

실습과정에서 필요에 따라 일부 강의 내용의 누락 및 추가, 수정사항이 있습니다.

퀴즈의 경우, 유튜브 풀이와 상이할 수 있습니다.


쿠팡 노트북 페이지별 정보

import requests
import re
from bs4  import BeautifulSoup

# 쿠팡 노트북 검색 결과를 페이지를 지정해서 출력
headers = {"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.104 Safari/537.36"}

# 페이지 선택
def coupang(page):
    # 페이지 정보 출력
    print("## 현재 페이지:" + str(page), "##")
    # 페이지에 따라 url 변경
    url = "https://www.coupang.com/np/search?q=%EB%85%B8%ED%8A%B8%EB%B6%81&channel=user&component=&eventCategory=SRP&trcid=&traid=&sorter=\
    scoreDesc&minPrice=&maxPrice=&priceRange=&filterType=&listSize=36&filter=&isPriceRange=false&brand=&offerCondition=&rating=0&page={0}&\
    rocketAll=false&searchIndexingToken=&backgroundColor=".format(page)

    res = requests.get(url, headers=headers)
    res.raise_for_status()

    soup = BeautifulSoup(res.text, "lxml")

    items = soup.find_all("li", attrs={"class":re.compile("^search-product")})

    for i in items:
        # 로켓 배송만 포함
        rocket = i.find("span", attrs={"class":"badge rocket"})
        if not rocket:
            print("로켓 배송만 포함합니다")
            continue
            
        # 삼성전자 상품 제외
        name = i.find("div", attrs={"class":"name"}).get_text() # 제품명
        if "삼성전자" in name:
            print("삼성전자 제품 제외")
            continue

        price = i.find("strong", attrs={"class":"price-value"}).get_text() # 가격

        # 리뷰 50개 이상, 평점 4.2 이상만 조회
        rate = i.find("em", attrs={"class":"rating"}) # 평점 - 평점이 없는 경우가 있었음
        if rate:
            rate = rate.get_text()
        else:
            print("평점 없음")
            continue

        rate_cnt = i.find("span", attrs={"class":"rating-total-count"}) # 평점 수 (30)
        if rate_cnt:
            rate_cnt = rate_cnt.get_text()[1:-1] # 평점 수 옆의 괄호 제거
        else:
            print("평점 수 없음")
            continue
        
        # 링크 추가
        link = i.find("a", attrs={"class":"search-product-link"})["href"]
        
        # 최중 출력
        if float(rate) >= 4.2 and int(rate_cnt) >= 50:
            print(f"제품명: {name}")
            print(f"가격: {price}원")
            print(f"평점: {rate} ({rate_cnt})개")
            print(f"링크: {'https://www.coupang.com' + link}")
            print("-"*100)

coupang(2)
## 현재 페이지:2 ##
제품명: LG전자 그램360 옵시디안 블랙 노트북 16TD90P-GX50K (i5-1135G7 40.6cm), 윈도우 미포함, 256GB, 8GB
가격: 1,679,000원
평점: 5.0 (54)개
링크: https://www.coupang.com/vp/products/5122034451?itemId=7002420105&vendorItemId=74294692462
----------------------------------------------------------------------------------------------------
제품명: 델 Inspiron 15 5505 플래티넘 실버 노트북 DN5505-WH01KR (라이젠5-4500U 39.6cm WIN10 Home), 윈도우 포함, 256GB, 8GB
가격: 740,590원
평점: 4.5 (206)개
링크: https://www.coupang.com/vp/products/1862432591?itemId=3165858124&vendorItemId=71153397923
----------------------------------------------------------------------------------------------------
제품명: 레노버 Flex 5 노트북 14ARE R3 W10S (라이젠3-4300U 35.5cm Radeon Graphics WIN10 S), 윈도우 포함, 128GB, 8GB
가격: 728,000원
평점: 4.5 (66)개
링크: https://www.coupang.com/vp/products/1734222912?itemId=2952308729&vendorItemId=70940828658
----------------------------------------------------------------------------------------------------
삼성전자 제품 제외
제품명: LG전자 울트라 화이트 노트북 14U390-ME1TK (셀러론-N4100 35.5cm WIN10 S), 포함, 192GB, 8GB
가격: 581,100원
평점: 4.5 (117)개
링크: https://www.coupang.com/vp/products/4841548763?itemId=6257652936&vendorItemId=73553348831
----------------------------------------------------------------------------------------------------
제품명: Apple 2020년 맥북 에어 13, M1 8-Core, 8GB, SSD 256GB, 골드
가격: 1,238,400원
평점: 5.0 (1272)개
링크: https://www.coupang.com/vp/products/4322481134?itemId=5033260880&vendorItemId=72343055474
----------------------------------------------------------------------------------------------------
삼성전자 제품 제외
평점 없음
제품명: 레노버 노트북 V14-ARE 82DQ003AKR (R5-4500U 35.56cm), 윈도우 미포함, 256GB, 4GB
가격: 479,000원
평점: 4.5 (231)개
링크: https://www.coupang.com/vp/products/1676672325?itemId=2856499921&vendorItemId=70845799268
----------------------------------------------------------------------------------------------------
삼성전자 제품 제외
제품명: Apple 2020년 맥북 프로 13 (M1 칩셋 8코어 CPU 8코어 GPU), 8GB, SSD 256GB, 실버
가격: 1,559,000원
평점: 5.0 (805)개
링크: https://www.coupang.com/vp/products/4322481029?itemId=5033260964&vendorItemId=72343055338
----------------------------------------------------------------------------------------------------
로켓 배송만 포함합니다
제품명: 레노버 아이디어패드 Ice Blue 노트북 S150-11 81VT (Intel Celeron N4020 29.5cm WIN10 Home S), 윈도우 포함, 64GB, 4GB
가격: 285,720원
평점: 4.5 (110)개
링크: https://www.coupang.com/vp/products/1547994000?itemId=2649830769&vendorItemId=70640681612
----------------------------------------------------------------------------------------------------
로켓 배송만 포함합니다
  • 이전 포스팅에서 쿠팡 노트북 검색시 첫 페이지 정보를 불러왔다.

  • 이번에는 url을 함수에 따라 변경시켜 여러 페이지에 대해 정보를 불러올 수 있게 작업하였다.

  • 각 태그별 정보는 직접 홈페이지에 들어가서 F12를 눌러 개발자 모드로 확인하면 재밌을 것 같다.

Leave a comment