🐍 Python으둜 ν…ŒμŠ€νŠΈ μ½”λ“œ μž‘μ„±ν•΄λ³΄κΈ°

이쀀석·2021λ…„ 5μ›” 9일
0

ν…ŒμŠ€νŠΈ

λͺ©λ‘ 보기
3/3
post-thumbnail

μ„œλ‘ 

μ „ μ‹œκ°„μ—λŠ” Selenium 을 μ‚¬μš©ν•˜κΈ° μœ„ν•œ ν™˜κ²½μ„ κ΅¬μ„±ν•˜μ˜€κ³ , 이번 μ‹œκ°„μ—λŠ” κ΅¬μ„±λœ Selenium 을 μ΄μš©ν•΄μ„œ μ‹€μ œλ‘œ ν…ŒμŠ€νŠΈ μ½”λ“œλ₯Ό μž‘μ„±ν•˜λŠ” 방법을 μ•Œμ•„ λ³Ό μ˜ˆμ •μ΄λ‹€. λ§Œμ•½μ— ν™˜κ²½ κ΅¬μ„±ν•˜λŠ” 방법에 λŒ€ν•΄μ„œ 아직 읽지 μ•Šμ€ 뢄은 μ½μœΌλ©΄μ„œ 찬찬히 ν™˜κ²½μ„ ꡬ성해보길 λ°”λž€λ‹€.

그럼 λ°”λ‘œ ν…ŒμŠ€νŠΈ μ½”λ“œ μž‘μ„± 방법에 λŒ€ν•΄μ„œ μ•Œμ•„λ³΄λ„λ‘ ν•˜μž!

ν…ŒμŠ€νŠΈ ν™˜κ²½μ€ λ‹€μŒκ³Ό κ°™λ‹€.

OS : Window 10 Pro
IDE : Pycharm Ultimate Edition
Python SDK : Python 3.8 (링크λ₯Ό λˆ„λ₯΄λ©΄ μ„€μΉ˜νŽ˜μ΄μ§€λ‘œ μ΄λ™ν•©λ‹ˆλ‹€.)
Web browser : Google Chrome 90 (Chrome ꢌμž₯)
Python Environment : virtualenv (pip)

λ³Έλ‘ 

λ¨Όμ € μžμ‹ μ΄ Selenium μ½”λ“œ μž‘μ„±μ„ μœ„ν•΄ μ‚¬μš©ν•˜λŠ” IDEλ₯Ό μ‹€ν–‰μ‹œν‚¨λ‹€. 그리고 이전에 λ§Œλ“€μ—ˆλ˜ ν”„λ‘œμ νŠΈλ₯Ό μΌ λ‹€. (ν•„μžλŠ” PyCharm 을 μ΄μš©ν•œλ‹€.)

μ—¬κΈ°μ„œ 잠깐!πŸ–
μž‘μ„±μ— μ•žμ„œμ„œ 많이 μ‚¬μš©λ˜λŠ” Selenium μ½”λ“œμ— λŒ€ν•΄μ„œ 짚고 λ„˜μ–΄κ°€ λ³Ό μ˜ˆμ •μ΄λ‹€.

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait


def main():
    # μžμ‹ μ΄ μ‚¬μš©ν•˜λŠ” μ›Ήλ“œλΌμ΄λ²„ κ°€μ Έμ˜€κΈ°
    driver = webdriver.Chrome("C:/Users/junsugi/Desktop/chromedriver.exe")
    # get λ©”μ„œλ“œλŠ” ν•΄λ‹Ή URL둜 μ΄λ™ν•œλ‹€.
    driver.get("https://google.com")
    # id둜 element 찾기
    driver.find_element_by_id("id")
    # class둜 ν•˜λ‚˜μ˜ element μ°ΎκΈ°
    driver.find_element_by_class_name("className")
    # class둜 μ—¬λŸ¬κ°œμ˜ element μ°ΎκΈ°
    driver.find_elements_by_class_name("className")
    # class 이외에도 name, tag name λ“± λ‹€μ–‘ν•œ μš”μ†Œλ‘œ elementλ₯Ό 찾을 수 μžˆλ‹€.
    # ν•„μš”ν•œ 상황에 λ”°λΌμ„œ μ§„ν–‰ν•˜λ©΄ 될 것 κ°™λ‹€.

    # xpath둜 element μ°ΎκΈ° (κΈ€μ—μ„œ μžμ„Ένžˆ λ‹€λ£° μ˜ˆμ •)
    driver.find_element_by_xpath("xpath")
    # νŽ˜μ΄μ§€ λ‘œλ”© 기닀릴 수 μžˆλ„λ‘ ν•΄μ£ΌλŠ” 라이브러리
    wait = WebDriverWait(driver, 5)

μƒκ°λ‚˜λŠ”κ±΄ 이정도 밖에 μ—†λ‹€. μΆ”κ°€λ‘œ 도움이 λ λ§Œν•œ λ‚΄μš©μ΄ μƒκ°ν•˜λ©΄ 적어넣도둝 ν•˜κ² μŠ΅λ‹ˆλ‹€.

보톡 Selenium μ½”λ“œλŠ” μžμ‹ μ΄ κ°œλ°œν•œ μ›Ή μ‚¬μ΄νŠΈ ν…ŒμŠ€νŠΈλ₯Ό μœ„ν•΄μ„œ μ‚¬μš©ν•˜μ§€λ§Œ μš°λ¦¬λŠ” μš°μ„  μ‹œμ€‘μ— λ‚˜μ™€μžˆλŠ” 유λͺ… μ‚¬μ΄νŠΈμ—μ„œ μ–΄λ–»κ²Œ μ½”λ“œλ₯Ό μ‚¬μš©ν•˜λŠ”μ§€λ₯Ό ν™•μΈν•΄λ³΄κ² μŠ΅λ‹ˆλ‹€. λ”°λΌμ„œ μ €λŠ” LostArk κ²Œμž„ ν™ˆνŽ˜μ΄μ§€μ—μ„œ μ €μ˜ κ²Œμž„ μ•„μ΄λ””μ˜ 정보λ₯Ό κ²€μƒ‰ν•˜κ³  데이터λ₯Ό κ°€μ Έμ˜€λŠ” μ‹œλ‚˜λ¦¬μ˜€λ‘œ ν•΄λ³΄κ² μŠ΅λ‹ˆλ‹€.

μš°μ„  λ‘œμŠ€νŠΈμ•„ν¬ κ³΅μ‹ν™ˆνŽ˜μ΄μ§€μ— μ ‘μ†ν•©λ‹ˆλ‹€. κ·Έλ¦¬κ³ λŠ” 개발자 도ꡬ(F12) λ₯Ό μΌœμ„œ μžμ‹ μ΄ μ°Ύμ•„μ•Ό ν•˜λŠ” element λ₯Ό μ°ΎμŠ΅λ‹ˆλ‹€. μ €λŠ” μ „νˆ¬μ •λ³΄μ‹€μ΄λΌλŠ” λ²„νŠΌμ˜ elementλ₯Ό μ°Ύκ² μŠ΅λ‹ˆλ‹€.

μ—¬κΈ°μ„œ λ‚΄κ°€ μ›ν•˜λŠ” element 의 속성을 λΉ λ₯΄κ²Œ μ°ΎλŠ” 방법은 개발자 도ꡬλ₯Ό 켜면 λ‹€μŒκ³Ό 같은 μ•„μ΄μ½˜μ΄ λ³΄μž…λ‹ˆλ‹€.

ν•΄λ‹Ή μ•„μ΄μ½˜μ„ λˆ„λ₯΄κ³  λ‚΄κ°€ μ›ν•˜λŠ” elementλ₯Ό ν΄λ¦­ν•˜λ©΄ κ³§λ°”λ‘œ κ°œλ°œμžλ„κ΅¬μ—μ„œ μœ„μ˜ κ·Έλ¦Όκ³Ό 같이 νŒŒλž€μƒ‰μœΌλ‘œ μ„ νƒν•΄μ€λ‹ˆλ‹€.

이 과정에 단좕기가 μ‘΄μž¬ν•˜λŠ”λ° Ctrl + Shite + C λ₯Ό λˆ„λ₯΄λ©΄ μ•„μ΄μ½˜μ„ λˆ„λ₯΄μ§€ μ•Šμ•„λ„ μžλ™μœΌλ‘œ Select λͺ¨λ“œλ‘œ μ „ν™˜λ©λ‹ˆλ‹€.

κ·Έλ¦¬κ³ λ‚˜μ„œ element λ₯Ό ν™•μΈν•΄λ΄€λ”λ‹ˆ ID 값도 μ—†κ³  Class, Name 값이 μ „λΆ€ μ—†μ–΄μ„œ μœ„μ—μ„œ 배운 μ½”λ“œλ‘œλŠ” element λ₯Ό 찾을 μˆ˜κ°€ μ—†λŠ”λ°, μ΄λ•Œ μ‚¬μš©ν•˜λŠ” 방법이 xPath μž…λ‹ˆλ‹€.

λ”°λΌμ„œ xPathλ₯Ό μ΄μš©ν•΄μ„œ λ‹€μŒμ˜ element λ₯Ό 찾으면 λ‹€μŒκ³Ό 같이 찾을 수 μžˆμŠ΅λ‹ˆλ‹€.

character = driver.find_element_by_xpath("//nav[@class='nav']//ul//a[@href='/Profile/Character']")
character.click()

μ™œ μ €λ ‡κ²Œ xPath κ°€ μž…λ ₯이 λ˜μ—ˆλŠ”μ§€ html 을 톡해 μ‚΄νŽ΄λ³΄μž.

μ „νˆ¬μ •λ³΄μ‹€ elementκ°€ λ“€μ–΄μžˆλŠ” 첫 μ‹œμž‘ νƒœκ·Έκ°€ div 둜 λ‚˜μ™€μžˆλ‹€. λ”°λΌμ„œ μ²˜μŒλΆ€ν„° 타고 λ“€μ–΄κ°€λŠ” 방법도 μžˆμ§€λ§Œ λ‚˜λŠ” nav νƒœκ·Έκ°€ μ΄κ³³μ—μ„œλ§Œ μ‚¬μš©λ˜λŠ”κ±Έ κ°œλ°œμžλ„κ΅¬μ—μ„œ κ²€μƒ‰ν•΄μ„œ μ•Œμ•˜κ³  nav λΆ€ν„° 타고 λ“€μ–΄κ°€λŠ” λ°©ν–₯으둜 νƒν–ˆλ‹€. 그런데 ν™•μ‹€ν•˜κ²Œ λ‚΄κ°€ μ›ν•˜λŠ” nav 둜 κ²€μƒ‰λ˜κΈΈ μ›ν–ˆκ³  λ”°λΌμ„œ class λ₯Ό λͺ…μ‹œν•΄μ€¬λ‹€.

λ”°λΌμ„œ //nav[@class='nav'] 둜 xPath 의 μŠ€νƒ€νŠΈλ₯Ό λŠμ—ˆλ‹€. 그리고 nav 밑에 μ‘΄μž¬ν•˜λŠ” νƒœκ·Έκ°€ h2 와 ul 이 μžˆλŠ”λ° h2 λŠ” 우리의 λͺ©μ κ³Ό 상관 μ—†λŠ” νƒœκ·Έλ‹ˆκΉŒ λ¬΄μ‹œν•˜κ³  ul νƒœκ·Έλ₯Ό μ—΄μ–΄λ³΄μž. 그러면 λ§Žμ€ 개수의 li 듀이 μžˆλŠ”λ° μš°μ„  μ—¬κΈ°κΉŒμ§€ ν•˜λ©΄ //nav//ul μ΄λ ‡κ²Œ μž‘μ„±μ΄ λœλ‹€.

μ—¬κΈ°κΉŒμ§€λŠ” κ·Έλƒ₯ μ†μ‰½κ²Œ 따라할 수 μžˆλ‹€. κ·Έ λ‹€μŒμ— μ—¬κΈ°μ„œ 핡심인데, li λ₯Ό νŠΉμ •ν•˜κ²Œ ꡬ뢄해쀄 수 μžˆλŠ” selector κ°€ μ—†λ‹€. κ·Έλž˜μ„œ ν•„μžκ°€ μƒκ°ν•œ 뢀뢄은 a νƒœκ·Έμ— href κ°’μœΌλ‘œ μ „νˆ¬μ •λ³΄μ‹€ element λ₯Ό μ°ΎλŠ”λ°©λ²•μ„ λ– μ˜¬λ Έλ‹€. κ·Έλž˜μ„œ xPath λŠ” λ‹€μŒκ³Ό 같이 적힐 수 μžˆλ‹€.

//nav[@class='nav']//ul//a[@href='/Profile/Character']

λ”°λΌμ„œ ul νƒœκ·Έ 밑에 a νƒœκ·Έλ₯Ό μ°ΎλŠ”λ° href λ‚΄μš©μ΄ /Profile/Character 인 a νƒœκ·Έλ₯Ό μ°Ύκ² λ‹€λŠ” μ˜λ―Έλ‹€. μ—¬κΈ°μ„œ ν•œκ°€μ§€ μ€‘μš”ν•œ 문법이 λ‚˜μ˜€λŠ”λ° νƒœκ·Έμ΄λ¦„[@μ…€λ ‰ν„°='κ°’'] 으둜 적으면 ν•΄λ‹Ή element λ₯Ό 찾을 수 μžˆλ‹€. μ…€λ ‰ν„°μ—λŠ” id, class, name, href λ“± λ‹€μ–‘ν•œ 값이 적힐 수 μžˆλŠ”λ° μžμ„Έν•œ λ‚΄μš©μ€ ꡬ글에 검색해보면 μ•Œ 수 μžˆλ‹€.

κ·Έλž˜μ„œ xPath λ₯Ό μ΅œμ’…μ μœΌλ‘œ 적어보면 λ‹€μŒ μ½”λ“œμ²˜λŸΌ 적힐 수 μžˆλ‹€.

character = driver.find_element_by_xpath("//nav[@class='nav']//ul//a[@href='/Profile/Character']")
character.click() # a νƒœκ·Έ ν΄λ¦­ν•˜κΈ°

find_elements_by_xpath κ°€ 있고 find_element_by_xpath κ°€ μžˆλŠ”λ° μš°λ¦¬λŠ” μ „νˆ¬μ •λ³΄μ‹€ ν•˜λ‚˜λ§Œ ν•„μš”ν•˜κΈ° λ•Œλ¬Έμ— element 둜 μ‚¬μš©ν•œλ‹€. ν˜Ήμ‹œ 볡수의 element λ₯Ό μ°Ύμ•„μ•Ό ν•˜λŠ” κ²½μš°λŠ” elements 둜 κ²€μƒ‰ν•œλ‹€.

그리고 μ‹€ν–‰ν•˜λ©΄ μ „νˆ¬μ •λ³΄μ‹€λ‘œ νŽ˜μ΄μ§€κ°€ μ΄λ™ν•˜λŠ” 것을 λ³Ό 수 μžˆλ‹€.

μ—¬κΈ°μ„œ λ‚΄ λ‹‰λ„€μž„μ„ κ²€μƒ‰ν•˜κ³  정보λ₯Ό print ν•΄λ³΄λŠ” λΆ€λΆ„κΉŒμ§€ μ§„ν–‰ν•΄λ³΄μž. xpath 뢀뢄이 μ„€λͺ…이 κΈΈμ–΄μ Έμ„œ κ·ΈλŸ¬μ§€ λ‚˜λ¨Έμ§€λŠ” κΈˆλ°©μ΄λ‹€.

λ”°λΌμ„œ λ‹€μ‹œ html 을 뢄석해야 λ˜λŠ”λ° λ‚΄κ°€ 값을 μž…λ ₯ν•΄μ•Ό λ˜λŠ” input νƒœκ·Έ element λ₯Ό μ°Ύμ•„μ•Ό ν•œλ‹€.

λ‘œλ°μ•„μ›ƒ γ„·γ„· πŸ’β€β™‚οΈ

λ‹€ν–‰(?)μŠ€λŸ½κ²Œλ„ input νƒœκ·Έμ—λŠ” 뭐가 많이 μ ν˜€μžˆλ‹€. λ”°λΌμ„œ 이 뢀뢄도 class λ₯Ό μ΄μš©ν•΄μ„œ element λ₯Ό 찾아보도둝 ν•˜κ² λ‹€. μ½”λ“œλŠ” λ‹€μŒκ³Ό κ°™λ‹€.

    input = driver.find_element_by_class_name("input--profile-search")
    input.send_keys("μ €κ±°μ’€")
    submit = driver.find_element_by_class_name("button--profile-search")
    submit.click()

send_keys λŠ” input νƒœκ·Έμ— 값을 넣을 λ•Œ μ‚¬μš©λœλ‹€. (μžμ„Έν•œ 뢀뢄은 좔후에 μ„€λͺ…)
click() λ©”μ„œλ“œλŠ” λ²„νŠΌμ„ 클릭할 λ•Œ μ‚¬μš©ν•œλ‹€.

μ—¬κΈ°κΉŒμ§€ μ‹€ν–‰ν•˜λ©΄ λ‹€μŒκ³Ό κ°™λ‹€.

λ§ˆμ§€λ§‰μœΌλ‘œ κ²€μƒ‰λœ 값을 print ν•΄μ„œ console에 찍어보면 λλ‚˜λŠ”λ°, ν•΄λ‹Ή 뢀뢄은 μ „λΆ€ λ‹€ λ˜‘κ°™κ³  .text λ©”μ„œλ“œλ§Œ μ΄μš©ν•˜λ©΄ λœλ‹€. λ”°λΌμ„œ κ°„λ‹¨ν•˜κ²Œ λ‹‰λ„€μž„ λ°μ΄ν„°λ§Œ κ°€μ Έμ˜€λ©΄ λ‹€μŒκ³Ό κ°™λ‹€.

    name = driver.find_element_by_class_name("profile-character-info__name")
    print(name.text)

μž‘μ•„μ„œ 보일지 λͺ¨λ₯΄κ² λŠ”데 console 창에 'μ €κ±°μ’€' 이 뜬 λͺ¨μŠ΅μ„ λ³Ό 수 μžˆλ‹€.

λ”°λΌμ„œ 이 과정을 전체 μ½”λ“œλ‘œ 적으면 λ‹€μŒκ³Ό κ°™λ‹€.

import time

from selenium import webdriver

def print_hi():
    # μžμ‹ μ΄ μ‚¬μš©ν•˜λŠ” μ›Ήλ“œλΌμ΄λ²„ κ°€μ Έμ˜€κΈ°
    driver = webdriver.Chrome("C:/Users/junsugi/Desktop/chromedriver.exe")
    # get λ©”μ„œλ“œλŠ” ν•΄λ‹Ή URL둜 μ΄λ™ν•œλ‹€.
    driver.get("https://lostark.game.onstove.com/Main")
    character = driver.find_element_by_xpath("//nav[@class='nav']//ul//a[@href='/Profile/Character']")
    character.click()

    input = driver.find_element_by_class_name("input--profile-search")
    input.send_keys("μ €κ±°μ’€")
    submit = driver.find_element_by_class_name("button--profile-search")
    submit.click()

    name = driver.find_element_by_class_name("profile-character-info__name")
    print(name.text)

    # driver session μ’…λ£Œ
    driver.close()

if __name__ == '__main__':
    print_hi()

κ²°λ‘ 

μ΅œλŒ€ν•œ μ‰½κ²Œ μ„€λͺ…을 ν•˜λ €κ³  ν–ˆλŠ”λ° λ­”κ°€ μ€‘κ΅¬λ‚œλ°© μ„€λͺ…ν•œ 것 κ°™μ•˜λ‹€. μ§€κΈˆκΉŒμ§€ μ„€λͺ…ν•œ 뢀뢄은 Selenium 의 기초의 기초만 μ„€λͺ…ν–ˆλ‹€. λ‚˜λ„ 아직 전문적인 지식을 가진건 μ—†μ§€λ§Œ 쑰금 더 μ•Œμ•„μ•Ό ν•  뢀뢄듀이 μ‘΄μž¬ν•œλ‹€.

λŒ€κ²Œ νŽ˜μ΄μ§€ λ‘œλ”©μ΄ λλ‚˜κΈ°λ„ 전에 element λ₯Ό μ°ΎλŠ” 경우 μ—λŸ¬κ°€ λœ¨λŠ”λ° κ·Έ λ•Œ νŽ˜μ΄μ§€ λ‘œλ”©μ„ κΈ°λ‹€λ Έλ‹€κ°€ element λ₯Ό μ°ΎλŠ” μ•„μ£Ό 쒋은 λΌμ΄λΈŒλŸ¬λ¦¬λ“€λ„ μžˆλ‹€.

이런 뢀뢄듀은 λ‹€μŒ ν¬μŠ€νŒ…μ—μ„œ 닀뀄보도둝 ν•  μ˜ˆμ •μ΄λ‹€.

λ‹€μŒ ν¬μŠ€νŒ…λ„ 재빠λ₯΄κ²Œ 올릴 수 μžˆλ„λ‘ λ…Έλ ₯ν•˜κ² μŠ΅λ‹ˆλ‹€.

κ°μ‚¬ν•©λ‹ˆλ‹€.

profile
호주 μ›Œν™€μ€‘

1개의 λŒ“κΈ€

comment-user-thumbnail
2021λ…„ 7μ›” 30일

쒋은 λ‚΄μš©μ΄λ„€μš” γ…Žγ…Ž

λ‹΅κΈ€ 달기