JJUNNAK's

[ Selenium ] 해외 축구사이트 크롤링 예제 본문

Crawling

[ Selenium ] 해외 축구사이트 크롤링 예제

최낙준 2023. 1. 13. 04:17

코드
 
from selenium import webdriver
from bs4 import BeautifulSoup

# 크롬 드라이버 경로
driver = webdriver.Chrome("C:/Users/nackjun/chromedriver.exe")
# 페이지 로드까지 3초 대기
driver.implicitly_wait(3)
# 해외 축구 사이트 불러옴.
driver.get("https://www.livesport.com/kr/team/chelsea/4fGZN2oK/")

# page에 드라이버에서 불러온 페이지의 html 저장
page = driver.page_source

site = BeautifulSoup(page,"html.parser")

# span 태그의 class속성 wld wld--'?' 파싱
win = site.find_all("span",{"class":"wld wld--w"})
draw = site.find_all("span",{"class":"wld wld--d"})
drawlose = site.find_all('span',{"class":"wld wld--lo"})
lose = site.find_all('span',{"class":"wld wld--l"})
# print(win)
# print(draw)
# print(drawlose)
# print(lose)

# 승,무,패 횟수
win_c = len(win)
draw_c = len(draw)
lose_c = len(lose) + len(drawlose)

# 경기기록 딕셔너리
record = {'win' : win_c, 'draw' : draw_c, 'lose' : lose_c }
print(record)


# 기록중 가장 큰값 max_c
max_c = max(record.values())
# record 안에 키값이 max_c 와 동일하면 key 출력
for key in record:
    if(record[key] == max_c):
        print(key)

 

 

 결과 

Comments