Python

[Python] Web Scraping

임호랑이 2021. 7. 13. 19:16

쿠팡에서 노트북을 사고싶다.

광고상품이 아니고
애플제품도 아니면서
평점이 4.5 이상이며
평점수는 100개 이상인
노트북만 추려서 갖고오고싶다.
라는 조건으로 웹 스크래핑을 시작해보려한다 

requests 라이브러리를 갖고오고
쿠팡 url을 넣는다
웹 스크래핑을 막아놨?을?수도?? 있기때문에

headers 에 User-Agent 도 추가해서 넣었다.

그후 BeautifulSoup 을 이용해서 데이터를 갖고온다

): 내 User-Agent 알아보기 
https://www.whatismybrowser.com/detect/what-is-my-user-agent

 

What is my user agent?

Every request your web browser makes includes your User Agent; find out what your browser is sending and what this identifies your system as.

www.whatismybrowser.com

 



items = soup.find_all("li", attrs={"class":re.compile("^search-product")})
#print(items[0].find("div", attrs={"class":"name"}).get_text())
for item in items:

    # 광고 제품은 제외
    ad_badge = item.find("span", attrs={"class":"ad-badge-text"})
    if ad_badge:
        print("  <광고 상품 제외합니다>")
        continue

    name = item.find("div", attrs={"class":"name"}).get_text() # 제품명

    # 애플 제품 제외
    if "Apple" in name:
        print("  <Apple 상품 제외합니다>")
        continue

    price = item.find("strong", attrs={"class":"price-value"}).get_text() # 가격
    
    # 리뷰 100개 이상, 평점 4.5 이상 되는 것만 조회
    rate = item.find("em", attrs={"class":"rating"}) # 평점
    if rate:
        rate = rate.get_text()
    else:
        print("  <평점 없는 상품 제외합니다>")
        continue

    rate_cnt = item.find("span", attrs={"class":"rating-total-count"}) # 평점 수 
    if rate_cnt:
        rate_cnt = rate_cnt.get_text() # 예 : (26)
        rate_cnt = rate_cnt[1:-1]
        #print("리뷰 수", rate_cnt)
    else:
        print("  <평점 수 없는 상품 제외합니다>")
        continue

    if float(rate) >= 4.5 and int(rate_cnt) >= 100:
        print(name, price, rate, rate_cnt)

 

끝..

'Python' 카테고리의 다른 글

[Python] requests 라이브러리  (0) 2021.07.05