Allen's 데이터 맛집

[네이버 웹툰-1] 전체, 평균 평점 구하기 본문

Programming/Web Scraping

[네이버 웹툰-1] 전체, 평균 평점 구하기

Allen93 2023. 8. 10. 21:47

이번엔 네이버 웹툰 페이지에서 웹툰의 제목과 평점을 가져오는 간단한 웹 스크래핑을 해보겠습니다:)

코드를 통해 웹툰의 제목과 해당 웹툰의 평점을 추출하고, 전체 평균 평점을 계산합니다.

 

import requests

from bs4 import BeautifulSoup

url = 'https://comic.naver.com/webtoon/list.nhn?titleId=64997'
res = requests.get(url)
res.raise_for_status()

soup = BeautifulSoup(res.text, 'lxml')
cartoons = soup.find_all('td', attrs = {'class':'title'})


#평점 구하기
total_rates = 0  #전체 평점
cartoons = soup.find_all('div', attrs={'class':'rating_type'})
for cartoon in cartoons :
    rate = cartoon.find('strong').get_text()
    print(rate)
    total_rates += float(rate)  #실수형 형태로  tatal_rates에 삽입

print('전체 평점 : ', total_rates)
print('평균 평점 :', total_rates / len(cartoons))

>>> 전체 평점 : 9.56

>>> 평균 평점 : 9.69

728x90