처음 Selenium 만져본날이다. 자축을 위한 기념사진
from selenium import webdriver
#브라우저 꺼짐 방지 옵션
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
# 브라우저 꺼짐 방지 옵션
chrome_options = Options()
chrome_options.add_experimental_option("detach", True)
driver = webdriver.Chrome(options=chrome_options)
# URL 열기
driver.maximize_window()
# Google에서 DAK.GG 검색
elem = driver.find_element(By.NAME, "q")
elem.send_keys("닥지지")
elem.send_keys(Keys.RETURN)
# DAK.GG 사이트 클릭
elem = driver.find_element(By.CSS_SELECTOR, "#rso a h3")
elem.click()
# DAK.GG 사이트 내의 발로란트 클릭
elem = driver.find_element(By.CSS_SELECTOR, '#__next > div > div > main > div:nth-child(3) > div:nth-child(2) > div:nth-child(1) > ul > li:nth-child(3) > a > div > span:nth-child(1) > img')
elem.click()
# 발로란트 패치노트 진입
elem = driver.find_element(By.XPATH, '//*[@id="__next"]/main/div[2]/section[1]/header/a')
elem.click()
driver.switch_to.window(driver.window_handles[-1])
# span 태그 선택
driver.implicitly_wait(10)
span_element = driver.find_element(By.XPATH, '//span[contains(text(),"발로란트 6.08 패치 노트")]')
# 패치 제목 txt 파일로 추출
patch_title = span_element.text
file_path = "output.txt"
with open(file_path, "w", encoding="utf-8") as file:
file.write(patch_title)
# 패치 내용 추출
div_element = driver.find_element(By.XPATH, '//*[@id="gatsby-focus-wrapper"]/section[2]/div[1]/div[5]')
patch_detail = div_element.text
with open(file_path, "a", encoding="utf-8") as file:
file.write(patch_detail)
좋아하는 게임인 발로란트 패치노트를 긁어보기 위해 코드를 짜봤다.
조잡해 보이지만 패치노트 title과 detail을 txt파일로 저장하는것까지 성공.
'Selenium' 카테고리의 다른 글
Python Selenium 무한스크롤 (3) | 2023.05.19 |
---|---|
Python Selenium with 원티드(1) (1) | 2023.05.19 |
Python Selenium with 네이버쇼핑(2) (1) | 2023.05.12 |
Python Selenium with 네이버쇼핑 (0) | 2023.05.11 |