본문 바로가기
Djnago/1차 프로젝트

닥터자르트 Dr.Jart+ 크롤링 Part 1

by UnoCode 2020. 8. 13.

part1 : 매인 페이지 가져오기

 

part2 :상세 페이지 가져오기

 

part3 : 리뷰 가져오기

 

해당 자료는 개인 프로젝트를 위해 사용했으며 절대 절대 상업용이나 홍보용으로 사용 안했음을 알려 드리고

 

(공부용) 개인 프로젝트가 끝난후 모든 크롤링 파일을 삭제 했음을 밝힙니다.

from bs4 import BeautifulSoup # 정적인 웹사이트 분석 툴
from selenium.webdriver.common.keys import Keys # 스크롤을 내기기 위한 도구
from selenium import webdriver # 동적인 웹사이트 분석 툴
import csv # 크롤링한 파일을 csv파일 형식으로 변한 하기 위한 도구
import time # 브라우져 버퍼링을 위한 도구

# selenium의 속도 개선을 위해 BackGround로 작업
chrome_option = webdriver.ChromeOptions()
chrome_option.add_argument("headless")
chrome_option.add_argument("-disable-gpu")
chrome_option.add_argument("lang-=ko_KR")

# Web driver 실행 + background 버전
driver = webdriver.Chrome(
    "/home/spectre/바탕화면/Python/crawling/python/chromedriver",
    chrome_options=chrome_option,
)

# 닥터자르트 제품 매인 페이지 Html 소스 얻기
driver.get("https://www.drjart.com/ko/prd/main")

# 변수 선언
data_list = []

# Html 코드 분석을 위해 BeautifulSoup 사용(seleniumd을 사용하셔도 무방합니다)
html = driver.page_source
bs = BeautifulSoup(html, "html.parser")

# 전체 상품 목록 가져오기 (해당 로직 복사해서 카테고리벼로 지정)
info_box = bs.find_all("li", {"class": "pdtItem"})
for info in info_box:
    sale = str()
    gift = str()
    best = str()
    new = str()
    price_sale = 0
    price = 0
    product_url = info["data-prdmngseq"]
    title = info.find("p", {"class": "opt_tit"}).text
    tag = info.find("p", {"class": "opt_msg"}).text
    image_url = info.find("span", {"class": "opt_thumb"}).find("img")["src"]
    flags = info.find_all("div", {"class": "opt_flag"})
    for flag in flags:
        if flag.find("span", {"class": "sale"}) == None:
            sale = None
        else:
            sale = flag.find("span", {"class": "sale"}).text
        if flag.find("span", {"class": "gift"}) == None:
            gift = None
        else:
            gift = flag.find("span", {"class": "gift"}).text
        if flag.find("span", {"class": "best"}) == None:
            best = None
        else:
            best = flag.find("span", {"class": "best"}).text
        if flag.find("span", {"class": "new"}) == None:
            new = None
        else:
            new = flag.find("span", {"class": "new"}).text
    prices = info.find_all("span", {"class": "cost"})
    for price in prices:
        if price.string == None:  # 세일 값이 있을 경유
            price_sale = price.contents[2].replace("\n", "").replace("\t", "")
            price = price.find("span", {"class": "normal"}).string
        else:  # 세일 값이 없을 경우
            price = price.string
    data_list.append(
        {
            "title": title,
            "tag": tag,
            "image_url": image_url,
            "sale": sale,
            "gift": gift,
            "best": best,
            "new": new,
            "price": price,
            "price_sale": price_sale,
            "product_url": f"https://www.drjart.com/ko/prd/view/{product_url}?activeTopGnb=all",
        }
    )
print(data_list)

# 해당 파일을 csv파일로 만들기
def csv_save(star_menu):
    f = open("dr_product_main.csv", "w", encoding="utf-8", newline="")
    wr = csv.writer(f)
    wr.writerow(
        [
            "title",
            "tag",
            "image_url",
            "sale",
            "gift",
            "best",
            "new",
            "product_url",
            "price",
            "price_sale",
        ]
    )
    for Info in star_menu:
        wr.writerow(
            [
                Info["title"],
                Info["tag"],
                Info["image_url"],
                Info["sale"],
                Info["gift"],
                Info["best"],
                Info["new"],
                Info["product_url"],
                Info["price"],
                Info["price_sale"],
            ]
        )

    f.close()


csv_save(data_list)

 

가져온 항목 

 

 

+ 다음 포스팅을 위해 

 

product_url = www.drjart.com/ko/prd/view/ 정보를 가져왔습니다.

 

다음 포스팅 까지 보스는 분은 위 코드의 내용을 모두 크롤링 해주세요.

 

 

'Djnago > 1차 프로젝트' 카테고리의 다른 글

닥터자르트 Dr.Jart+ 크롤링 Part 3  (0) 2020.08.28
닥터자르트 Dr.Jart+ 크롤링 Part 2  (0) 2020.08.14
wecode 1차 프로젝트 후기  (0) 2020.08.05
[1차] 스크럼 방법  (0) 2020.07.22