|
| 1 | +import json |
| 2 | +import os |
| 3 | +from django.conf import settings |
| 4 | +from django.core.management.base import BaseCommand |
| 5 | +from selenium import webdriver |
| 6 | +from selenium.webdriver.common.by import By |
| 7 | +from selenium.webdriver.chrome.service import Service |
| 8 | +from selenium.webdriver.support.ui import WebDriverWait |
| 9 | +from selenium.webdriver.support import expected_conditions as EC |
| 10 | +from webdriver_manager.chrome import ChromeDriverManager |
| 11 | + |
| 12 | + |
| 13 | +class Command(BaseCommand): |
| 14 | + help = "Scrape products and save JSON under STATIC_ROOT/data/advertise_products.json" |
| 15 | + |
| 16 | + def handle(self, *args, **options): |
| 17 | + chrome_options = webdriver.ChromeOptions() |
| 18 | + chrome_options.add_argument("--headless") |
| 19 | + chrome_options.add_argument("--no-sandbox") |
| 20 | + chrome_options.add_argument("--disable-dev-shm-usage") |
| 21 | + chrome_options.add_argument("--disable-gpu") |
| 22 | + chrome_options.add_argument( |
| 23 | + 'user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36' |
| 24 | + ) |
| 25 | + |
| 26 | + driver = webdriver.Chrome( |
| 27 | + service=Service(ChromeDriverManager().install()), |
| 28 | + options=chrome_options |
| 29 | + ) |
| 30 | + |
| 31 | + try: |
| 32 | + url = "https://www.oliveyoung.co.kr/store/main/getBestList.do?dispCatNo=900000100100001&fltDispCatNo=10000010002&pageIdx=1&rowsPerPage=10" |
| 33 | + driver.get(url) |
| 34 | + wait = WebDriverWait(driver, 15) |
| 35 | + |
| 36 | + # Wait for <ul class="cate_prd_list"> to appear (parent of products) |
| 37 | + wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, 'ul.cate_prd_list'))) |
| 38 | + # Now get all <li class="flag"> |
| 39 | + products = driver.find_elements(By.CSS_SELECTOR, 'li.flag') |
| 40 | + |
| 41 | + all_products = [] |
| 42 | + for item in products: |
| 43 | + try: |
| 44 | + prd_info = item.find_element(By.CSS_SELECTOR, 'div.prd_info') |
| 45 | + |
| 46 | + # Main image and link |
| 47 | + link_tag = prd_info.find_element(By.CSS_SELECTOR, 'a.prd_thumb') |
| 48 | + product_url = link_tag.get_attribute('href') |
| 49 | + img_tag = link_tag.find_element(By.TAG_NAME, 'img') |
| 50 | + image_src = img_tag.get_attribute('src') |
| 51 | + image_alt = img_tag.get_attribute('alt') |
| 52 | + |
| 53 | + # Brand and product name |
| 54 | + prd_name_box = prd_info.find_element(By.CSS_SELECTOR, 'div.prd_name') |
| 55 | + brand_name = '' |
| 56 | + product_name = '' |
| 57 | + try: |
| 58 | + brand_name = prd_name_box.find_element(By.CSS_SELECTOR, 'span.tx_brand').text.strip() |
| 59 | + except Exception: |
| 60 | + pass |
| 61 | + try: |
| 62 | + product_name = prd_name_box.find_element(By.CSS_SELECTOR, 'p.tx_name').text.strip() |
| 63 | + except Exception: |
| 64 | + pass |
| 65 | + |
| 66 | + # Price |
| 67 | + price_original = '' |
| 68 | + price_current = '' |
| 69 | + try: |
| 70 | + price_box = prd_info.find_element(By.CSS_SELECTOR, 'p.prd_price') |
| 71 | + price_original_elem = price_box.find_elements(By.CSS_SELECTOR, 'span.tx_org') |
| 72 | + price_original = price_original_elem[0].text.strip() if price_original_elem else '' |
| 73 | + price_current_elem = price_box.find_elements(By.CSS_SELECTOR, 'span.tx_cur') |
| 74 | + price_current = price_current_elem.text.strip() if price_current_elem else '' |
| 75 | + except Exception: |
| 76 | + pass |
| 77 | + |
| 78 | + # Flags |
| 79 | + flags = [] |
| 80 | + try: |
| 81 | + prd_flag_box = prd_info.find_element(By.CSS_SELECTOR, 'p.prd_flag') |
| 82 | + flag_spans = prd_flag_box.find_elements(By.CSS_SELECTOR, 'span.icon_flag') |
| 83 | + flags = [flag.text.strip() for flag in flag_spans] |
| 84 | + except Exception: |
| 85 | + pass |
| 86 | + |
| 87 | + # Review score |
| 88 | + review_score = None |
| 89 | + try: |
| 90 | + prd_point_box = prd_info.find_element(By.CSS_SELECTOR, 'p.prd_point_area') |
| 91 | + review_point_elem = prd_point_box.find_elements(By.CSS_SELECTOR, 'span.review_point') |
| 92 | + if review_point_elem: |
| 93 | + score_text = review_point_elem[0].text.strip() |
| 94 | + if '10점만점에' in score_text and '점' in score_text: |
| 95 | + review_score = score_text.replace('10점만점에', '').replace('점', '').strip() |
| 96 | + except Exception: |
| 97 | + pass |
| 98 | + |
| 99 | + product_data = { |
| 100 | + 'product_url': product_url, |
| 101 | + 'brand_name': brand_name, |
| 102 | + 'product_name': product_name, |
| 103 | + 'image_src': image_src, |
| 104 | + 'image_alt': image_alt, |
| 105 | + 'price_original': price_original, |
| 106 | + 'price_current': price_current, |
| 107 | + 'flags': flags, |
| 108 | + 'review_score': review_score |
| 109 | + } |
| 110 | + |
| 111 | + all_products.append(product_data) |
| 112 | + except Exception as e: |
| 113 | + self.stderr.write(f"Error scraping item: {e}") |
| 114 | + continue |
| 115 | + |
| 116 | + SAVE_PATH = os.path.join(settings.BASE_DIR, 'static', 'data', 'advertise_products.json') |
| 117 | + os.makedirs(os.path.dirname(SAVE_PATH), exist_ok=True) |
| 118 | + with open(SAVE_PATH, 'w', encoding='utf-8') as f: |
| 119 | + json.dump(all_products, f, ensure_ascii=False, indent=2) |
| 120 | + print(f"\nSaved {len(all_products)} products to {SAVE_PATH}") |
| 121 | + |
| 122 | + finally: |
| 123 | + driver.quit() |
0 commit comments