본문 바로가기
데이터 관련/크롤링

혼자서 해보는 LOL 챔프 크롤링

by 준쓰_ 2021. 4. 29.

 

 

 

 

 

기본 구조

In [1]:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys # 엔터키 입력용
import pandas as pd
import time

driver = webdriver.Chrome('C:/Temp/chromedriver') # 웹드라이버 객체 생성
In [16]:
# 우선 수집하는 것에만 초점을 맞춰서 가능한지 확인합니다. 
# 확인 코드

# 구글을 열어서 키워드 입력 후 이동
driver.get('https://www.op.gg/champion/statistics') # 구글 홈페이지 내용 렌더링
time.sleep(2)

# 가렌 선택 이동
champ = driver.find_element_by_css_selector('body > div.l-wrap.l-wrap--champion > div.l-container > div.l-champion-index > div.l-champion-index-content > div.l-champion-index-content--main > div.champion-index__champion-list > div:nth-child(1) > a > div.champion-index__champion-item__image > i')
champ.click()


# 챔피언
champion_name = []
champion_position = []
champion_position_percentage = []
counter_champion = [] # 카운터 챔피언
counter_champion_percentage_list = []
cross_counter_champion = [] # 상대하기 쉬운 챔피언
cross_counter_champion_percentage_list = []


# 챔피언 이름
name_address = 'body > div.l-wrap.l-wrap--champion > div.l-container > div > div.l-champion-statistics-header > div > div.champion-stats-header-info.champion-stats-header-info--tier-2 > h1'
name = driver.find_element_by_css_selector(name_address)
champion_name.append(name.text)



# 챔피언 포지션
position_address = 'body > div.l-wrap.l-wrap--champion > div.l-container > div > div.l-champion-statistics-header > div > ul > li.champion-stats-header__position.champion-stats-header__position--active > a > span.champion-stats-header__position__role'
position = driver.find_element_by_css_selector(position_address)
champion_position.append(position.text)


# 챔피언 포지션 승률
position_percentage_address = 'body > div.l-wrap.l-wrap--champion > div.l-container > div > div.l-champion-statistics-header > div > ul > li.champion-stats-header__position.champion-stats-header__position--active > a > span.champion-stats-header__position__rate'
position_percentage = driver.find_element_by_css_selector(position_percentage_address)
champion_position_percentage.append(position_percentage.text)



# 카운터 챔피언
counter_list = []
for x in range(1,4):
    counter_champion_name_address = 'body > div.l-wrap.l-wrap--champion > div.l-container > div > div.l-champion-statistics-header > div > div.champion-stats-header-matchup.tabWrap._recognized > div > table.champion-stats-header-matchup__table.champion-stats-header-matchup__table--strong.tabItem > tbody > tr:nth-child('+str(x)+') > td.champion-stats-header-matchup__table__champion'
    counter_champion_name = driver.find_element_by_css_selector(counter_champion_name_address)
    counter_list.append(counter_champion_name.text)
counter_champion.append(counter_list)



# 카운터 챔피언 승률 
counter_percentage_list = []
for y in range(1,4):
    counter_champion_percentage_address = 'body > div.l-wrap.l-wrap--champion > div.l-container > div > div.l-champion-statistics-header > div > div.champion-stats-header-matchup.tabWrap._recognized > div > table.champion-stats-header-matchup__table.champion-stats-header-matchup__table--strong.tabItem > tbody > tr:nth-child('+str(y)+') > td.champion-stats-header-matchup__table__winrate > b'
    counter_champion_percentage = driver.find_element_by_css_selector(counter_champion_percentage_address)
    counter_percentage_list.append(counter_champion_percentage.text)
counter_champion_percentage_list.append(counter_percentage_list)


# 상대하기 쉬운 챔피언은 클릭으로 페이지 내 동적구현을 이용해야 합니다.
# 클릭해서 옮겨봅시다.
cross_counter_champion_button = driver.find_element_by_css_selector('body > div.l-wrap.l-wrap--champion > div.l-container > div > div.l-champion-statistics-header > div > div.champion-stats-header-matchup.tabWrap._recognized > ul > li:nth-child(2) > a')
cross_counter_champion_button.click()


# 상대하기 쉬운 챔피언 이름
cross_counter_list = []
for z in range(1,4):
    cross_counter_champion_name_address = 'body > div.l-wrap.l-wrap--champion > div.l-container > div > div.l-champion-statistics-header > div > div.champion-stats-header-matchup.tabWrap._recognized > div > table.champion-stats-header-matchup__table.champion-stats-header-matchup__table--weak.tabItem > tbody > tr:nth-child('+str(z)+') > td.champion-stats-header-matchup__table__champion'
    cross_counter_champion_name = driver.find_element_by_css_selector(cross_counter_champion_name_address)
    cross_counter_list.append(cross_counter_champion_name.text)
cross_counter_champion.append(cross_counter_list)



# 상대하기 쉬운 챔피언 승률 
cross_counter_percentage_list = []
for a in range(1,4):
    cross_counter_champion_percentage_address = 'body > div.l-wrap.l-wrap--champion > div.l-container > div > div.l-champion-statistics-header > div > div.champion-stats-header-matchup.tabWrap._recognized > div > table.champion-stats-header-matchup__table.champion-stats-header-matchup__table--weak.tabItem > tbody > tr:nth-child('+str(a)+') > td.champion-stats-header-matchup__table__winrate > b'
    cross_counter_champion_percentage = driver.find_element_by_css_selector(cross_counter_champion_percentage_address)
    cross_counter_percentage_list.append(cross_counter_champion_percentage.text)
cross_counter_champion_percentage_list.append(cross_counter_percentage_list)




print("챔피언 이름 :",champion_name)
print("챔피언 포지션 :",champion_position)
print("챔피언 포지션 승률 :",champion_position_percentage)
print("카운터 챔피언 이름 :",counter_champion)
print("카운터 챔피언 승률 :",counter_champion_percentage_list)
print("상대하기 쉬운 챔피언 이름 :",cross_counter_champion)
print("상대하기 쉬운 챔피언 승률 :",cross_counter_champion_percentage_list)
 
챔피언 이름 : ['가렌']
챔피언 포지션 : ['탑']
챔피언 포지션 승률 : ['75.31%']
카운터 챔피언 이름 : [['카밀', '나르', '다리우스']]
카운터 챔피언 승률 : [['48.17%', '48.52%', '48.62%']]
상대하기 쉬운 챔피언 이름 : [['하이머딩거', '요네', '그웬']]
상대하기 쉬운 챔피언 승률 : [['57.73%', '57.61%', '57.13%']]
In [ ]:
 

'데이터 관련 > 크롤링' 카테고리의 다른 글

구글 뉴스기사 크롤링 (Selenium 문법 변경)  (0) 2023.12.31