[python] #9. 네이버 KBO 순위표 크롤링 하기

exoluse·2021년 10월 12일
0

python - web crawling

목록 보기
10/20
post-thumbnail

일단 순위 페이지로 이동

원하는 데이터가 있는 곳을 찍자

엘리먼트 id가 regularTeamRecordList_table 라는 것을 확인

여기까지 구현해보면 이러하다.

import requests
from bs4 import BeautifulSoup

html = requests.get("https://sports.news.naver.com/kbaseball/record/index?category=kbo")
soup = BeautifulSoup(html.content, "html.parser")

selected = soup.find_all(id="regularTeamRecordList_table")[0]

print(selected)

<!-- 결과 : 테이블이 이어진다. -->
<tbody id="regularTeamRecordList_table">
<tr>
<th><strong>1</strong></th>
<td class="tm">
<div>
<img alt="구단로고" height="25" onerror="javaScript:noTeamEmblemImg(this, 'team', 'KT');" src="https://dthumb-phinf.pstatic.net/?src=https://sports-phinf.pstatic.net/team/kbo/default/KT.png&amp;type=f25_25&amp;refresh=1" width="25"/>
<span id="team_KT">KT</span>
</div>
</td>
<td><span>129</span></td>
<td><span>71</span></td>
<td><span>51</span></td>
<td><span>7</span></td>
<td><strong>0.582</strong></td>
<td><span>0.0</span></td>
<td><span>1패</span></td>
<td><span>0.358</span></td>
<td><span>0.383</span></td>
<td><span>3승-5패-2무</span></td>
</tr>
...
...
...

데이터는 대충 확인했으니 찍먹을 해보자

html = requests.get("https://sports.news.naver.com/kbaseball/record/index?category=kbo")
soup = BeautifulSoup(html.content, "html.parser")

selected = soup.find_all(id="regularTeamRecordList_table")[0]

rows = selected.find_all("tr")

for item in rows :
    print(item.find_all("th")[0].text)

<!-- 결과 : 순위만 리턴 되었다. -->
1
2
3
4
5
6
7
8
9
10

tr 내부의 데이터를 하나씩 확인한다

6번째 td를 제외하고는 나머지는 span 태그이다. 그냥 text 로 처리해도 될 듯 하다.

html = requests.get("https://sports.news.naver.com/kbaseball/record/index?category=kbo")
soup = BeautifulSoup(html.content, "html.parser")

selected = soup.find_all(id="regularTeamRecordList_table")[0]

rows = selected.find_all("tr")

for item in rows :
    # tr
    print(item.find_all("th")[0].text + "위 : "  # rank
            +item.find_all("div")[0].text # team name
            +"("
            +"경기수 : "+item.find_all("td")[1].text
            +"\t승 : "+item.find_all("td")[2].text
            +"\t패 : "+item.find_all("td")[3].text
            +"\t무 : "+item.find_all("td")[4].text
            +"\t승률 : "+item.find_all("td")[5].text
            +"\t게임차 : "+item.find_all("td")[6].text
            +"\t연속 : "+item.find_all("td")[7].text
            +"\t출루율 : "+item.find_all("td")[8].text
            +"\t장타율 : "+item.find_all("td")[9].text
            +"\t최근10경기 : "+item.find_all("td")[10].text
            +")"
    )

<!-- 결과 : 이정도면 거의 완벽하다. -->
1위     KT      (경기수 : 129   승 : 71 패 : 51 무 : 7  승률 : 0.582    게임차 : 0.0    연속 : 1패      출루율 : 0.358  장타율 : 0.383  최근10경기 : 3승-5패-2무)
2위     삼성    (경기수 : 131   승 : 69 패 : 54 무 : 8  승률 : 0.561    게임차 : 2.5    연속 : 1승      출루율 : 0.346  장타율 : 0.401  최근10경기 : 5승-5패-0무)
3위     LG      (경기수 : 126   승 : 66 패 : 52 무 : 8  승률 : 0.559    게임차 : 3.0    연속 : 1패      출루율 : 0.343  장타율 : 0.379  최근10경기 : 4승-3패-3무)
4위     두산    (경기수 : 127   승 : 63 패 : 59 무 : 5  승률 : 0.516    게임차 : 8.0    연속 : 1승      출루율 : 0.351  장타율 : 0.398  최근10경기 : 5승-5패-0무)
5위     키움    (경기수 : 129   승 : 62 패 : 61 무 : 6  승률 : 0.504    게임차 : 9.5    연속 : 1승      출루율 : 0.348  장타율 : 0.375  최근10경기 : 4승-4패-2무)
6위     SSG     (경기수 : 132   승 : 60 패 : 60 무 : 12 승률 : 0.500    게임차 : 10.0   연속 : 2승      출루율 : 0.353  장타율 : 0.419  최근10경기 : 4승-3패-3무)
7위     NC      (경기수 : 126   승 : 59 패 : 60 무 : 7  승률 : 0.496    게임차 : 10.5   연속 : 1패      출루율 : 0.347  장타율 : 0.423  최근10경기 : 4승-3패-3무)
8위     롯데    (경기수 : 130   승 : 60 패 : 65 무 : 5  승률 : 0.480    게임차 : 12.5   연속 : 2패      출루율 : 0.359  장타율 : 0.398  최근10경기 : 6승-3패-1무)
9위     KIA     (경기수 : 127   승 : 50 패 : 69 무 : 8  승률 : 0.420    게임차 : 19.5   연속 : 1패      출루율 : 0.337  장타율 : 0.335  최근10경기 : 5승-3패-2무)
10위    한화    (경기수 : 133   승 : 47 패 : 76 무 : 10 승률 : 0.382    게임차 : 24.5   연속 : 4패      출루율 : 0.337  장타율 : 0.347  최근10경기 : 3승-6패-1무)

0개의 댓글