๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Programming Basics

[ํฌ๋กค๋ง] ์›น ์‡ผํ•‘๋ชฐ์—์„œ ์ƒํ’ˆ์˜ ์žฌ๊ณ  ์—ฌ๋ถ€ ํ™•์ธํ•˜๊ธฐ(python)

by ISLA! 2024. 2. 28.

 

 

๐Ÿง‘๐Ÿป‍๐Ÿ’ป ์˜ฅ์…˜ ์ด๋ฒ ์ด์—์„œ ์ฃผ๊ธฐ์ ์œผ๋กœ ์ง๊ตฌํ•˜๋Š” ์ƒํ’ˆ์ด ์žˆ๋Š”๋ฐ..

์ด๊ฒŒ ์žฌ๊ณ ๊ฐ€ ์žˆ์„ ๋•Œ๊ฐ€ ์žˆ๊ณ  ์—†์„ ๋•Œ๊ฐ€ ์žˆ์–ด์„œ ๋งค๋ฒˆ ํ™•์ธํ•˜๊ธฐ๊ฐ€ ๊ท€์ฐฎ์•˜๋‹ค.ใ…Ž

 

 

๊ฐ„๋‹จํžˆ ๋‹ค์Œ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰์‹œ์ผœ ์›ํ•˜๋Š” ์ƒํ’ˆ๋ช…์„ ์ž…๋ ฅํ–ˆ์„ ๋•Œ,

์žฌ๊ณ ๊ฐ€ ์žˆ์œผ๋ฉด(์ƒํ’ˆ๋ฆฌ์ŠคํŠธ๊ฐ€ ์กด์žฌํ•˜๋ฉด) '๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.'๋ผ๊ณ  ํ”„๋ฆฐํŠธํ•˜๋Š” ํฌ๋กค๋ง ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ–ˆ๋‹ค.

์ฝ”๋“œ ๊ตฌ์„ฑ์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค

 

  • ์‡ผํ•‘๋ชฐ์€ ์˜ฅ์…˜ ์ด๋ฒ ์ด๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ž‘์„ฑ
  • ์›น ์‚ฌ์ดํŠธ๋ฅผ ์˜คํ”ˆํ•œ ํ›„, ๊ด‘๊ณ  ํŒ์—…์ด ๋œจ๋ฉด ๋‹ซ๋Š” ๊ณผ์ •์„ ์ถ”๊ฐ€
  • search_box ๋ณ€์ˆ˜์— ๊ฒ€์ƒ‰์–ด ์ฐฝ์˜ xpath๋ฅผ ์ž…๋ ฅ
  • search_box.send_keys() ์— ์žฌ๊ณ ๋ฅผ ํ™•์ธํ•˜๊ณ ์ž ํ•˜๋Š” ์ƒํ’ˆ๋ช…(๋˜๋Š” ํ‚ค์›Œ๋“œ) ์ž…๋ ฅ
  • ๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ์— ์ƒํ’ˆ์˜ ๋ฆฌ์ŠคํŠธ์˜ ์กด์žฌ ์—ฌ๋ถ€์— ๋”ฐ๋ผ ๊ฒฐ๊ณผ๋ฅผ print
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time

# ์›น ๋“œ๋ผ์ด๋ฒ„ ๊ฒฝ๋กœ ์„ค์ •
driver_path = '/your/driver/path'  # ์‚ฌ์šฉํ•˜๋Š” ์›น ๋ธŒ๋ผ์šฐ์ €์— ๋งž๊ฒŒ ๊ฒฝ๋กœ ์„ค์ •

# ์›น ๋“œ๋ผ์ด๋ฒ„ ์‹คํ–‰
driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()))

# ์‡ผํ•‘๋ชฐ ์›น ํŽ˜์ด์ง€๋กœ ์ด๋™
driver.get("https://es.auction.co.kr/")

# ๊ด‘๊ณ  ํŒ์—…์ด ์žˆ์œผ๋ฉด ๋‹ซ์Œ
try:
    WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="overlayLayer"]/div/div[1]'))).click()
except:
    pass

# ์ƒํ’ˆ๋ช…์„ ์ž…๋ ฅํ•  ๊ฒ€์ƒ‰์ฐฝ์„ ์ฐพ์•„์„œ ์ž…๋ ฅ
search_box = driver.find_element(By.XPATH, '/html/body/div[1]/div[1]/div[2]/div/div/form/input[3]')
search_box.send_keys("yoga")  # ์—ฌ๊ธฐ์— ์›ํ•˜๋Š” ์ƒํ’ˆ๋ช…์„ ์ž…๋ ฅ

# ๊ฒ€์ƒ‰ ์‹คํ–‰
search_button = driver.find_element(By.XPATH, '/html/body/div[1]/div[1]/div[2]/div/div/form/button')
driver.execute_script("arguments[0].click();", search_button)

# ํŽ˜์ด์ง€๊ฐ€ ๋กœ๋“œ๋˜๋Š” ์‹œ๊ฐ„์„ ๊ธฐ๋‹ค๋ฆฐ ํ›„์— ์ƒํ’ˆ ๋ฆฌ์ŠคํŠธ๊ฐ€ ๋‚˜์˜ค๋Š”์ง€ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Œ
time.sleep(5)  # ํŽ˜์ด์ง€ ๋กœ๋“œ๋ฅผ ๊ธฐ๋‹ค๋ฆฌ๊ธฐ ์œ„ํ•ด ์ž ์‹œ ๋Œ€๊ธฐ (์ดˆ ๋‹จ์œ„)

# ๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ ํ™•์ธ
try:
    search_result = driver.find_element(By.XPATH, '/html/body/div[1]/form/div/div[1]/div[3]/div[2]/div[2]')
    items = search_result.find_elements(By.XPATH, './div')  # ๊ฐ ์ƒํ’ˆ์„ ๋‚˜ํƒ€๋‚ด๋Š” ์š”์†Œ๋“ค์„ ์ฐพ์Œ
    if items:
        print("๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.")
        # ์—ฌ๊ธฐ์— ๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ ์ฒ˜๋ฆฌ ๋กœ์ง์„ ์ถ”๊ฐ€
    else:
        print("๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค.")
        # ๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ๊ฐ€ ์—†์„ ๋•Œ ์ฒ˜๋ฆฌํ•˜๋Š” ๋กœ์ง์„ ์ถ”๊ฐ€
except Exception as e:
    print("๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค:", e)
    
# ์›น ๋ธŒ๋ผ์šฐ์ € ์ข…๋ฃŒ
driver.quit()

 

 

์‹คํ–‰ ๊ฒฐ๊ณผ ์˜ˆ์‹œ๐Ÿ‘‡

 

728x90