# 크롤링할 때 기본 코드 (그대로 사용한다고 보면 된다.)
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
chrome_options = Options()
chrome_options.add_experimental_option("detach", True)
chrome_options.add_experimental_option("excludeSwitches", ["enable-logging"])
driver = webdriver.Chrome(options=chrome_options)
# 예시로 Google의 날씨 정보 페이지로 이동
driver.get("https://www.google.com/search?q=weather")
# CSS 선택자를 이용해서 원하는 클래스를 가져온다.
element = driver.find_element(By.ID, "wob_tm").text
loc = driver.find_element(By.CSS_SELECTOR, 'span.BBwThe').text
print(f'현재 {loc}의 온도는 {element}도!')
위 코드 작성 후 실행해보면
이렇게 확인할 수 있다.
위에 코드에는 없지만, selenium webdriver 를 사용했다면, 마지막에 아래 코드를 넣어 종료해주도록 하자!!
# 드라이버 종료
driver.quit()
'🔍QA & TEST > 🧑🏻💻Python' 카테고리의 다른 글
[팀 스파르타-업무자동화] 3주차 - openpyxl 라이브러리 (0) | 2024.08.18 |
---|---|
[팀 스파르타-업무자동화] 3주차 - 네이버 쇼핑 크롤링 (0) | 2024.08.18 |
[Python] 날개 달기 : 예외 처리 (try-except/finally/else) (0) | 2024.07.27 |
[Python] 날개 달기 : 패키지 (Package) (0) | 2024.07.26 |
[Python] 날개 달기 : 모듈 (Module) (0) | 2024.07.26 |