플그래밍/파이써언

[파이썬] Selenium - 네이버 스포츠 일정 및 결과 크롤링하기

훗티v 2024. 2. 2. 17:25
728x90
반응형

이번에는 네이버 스포츠 사이트의 일별 야구 일정/결과 페이지를 크롤링해볼께요

 

네이버 스포츠

 

 

먼저 Selenium을 사용하여 해당 사이트로 접속합니다.

import requests
from bs4 import BeautifulSoup
from selenium import webdriver
import undetected_chromedriver as uc
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import Select
from subprocess import CREATE_NO_WINDOW
import time

url = 'https://m.sports.naver.com/kbaseball/schedule/index?date=2023-09-24'

option = Options()
option.add_argument('--disable-gpu')
option.add_argument('--window-size=1920x1080')
option.add_argument('--start-maximized')
option.add_argument('user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36')

service = Service()
service.creation_flags = CREATE_NO_WINDOW
driver = uc.Chrome(service=service, options=option)
driver.maximize_window()
driver.implicitly_wait(10)
driver.get(url)

 

 

리그별로 분류되어있는 스코어보드를 크롤링해볼께요

divs = driver.find_elements(By.CLASS_NAME, "ScheduleAllType_match_list_group__1nFDy")

for div in divs:

    partial_class_name = "MatchBox_match_area"
    matching_divs = div.find_elements(By.CSS_SELECTOR, f'div[class*="{partial_class_name}"]')

    # Iterate over the matching divs
    for match in matching_divs:
        print(match.text.replace("\n", ", "))
        print("-----------------------------"*3)

driver.quit()

 

 

크롤링 타겟 페이지

 

 

크롤링 결과

 

 

질문은 댓글창에 남겨주세요~

 

 

반응형