728x90
반응형
# 리뷰 정보 클래스 작성하기
import urllib.request
from bs4 import BeautifulSoup
class Review:
def __init__(self, comment, date, star, good, bad):
self.comment = comment
self.date = date
self.star = star
self.good = good
self.bad = bad
def show(self):
print("내용: " + self.comment +
"\n날짜: " + self.date +
"\n별점: " + self.star +
"\n좋아요: " + self.good +
"\n싫어요: " + self.bad)
# 리뷰 정보 크롤링 함수
def crawl(url):
# urllib.request : URL(Uniform Resource Locator)을 가져오기 위한 파이썬 모듈
# urlopen 함수의 형태로, 매우 간단한 인터페이스를 제공함
soup = BeautifulSoup(urllib.request.urlopen(url).read(), "html.parser")
review_list = []
title = soup.find('h3', class_='h_movie').find('a').text
div = soup.find("div", class_="score_result")
data_list = div.select("ul > li")
for review in data_list:
# strip : string의 제일 앞 또는 끝의 내용이 인자와 일치할 경우 삭제해주고, 만일 인자를 넣지 않으면 \n(줄넘김), 빈공백을 없애줌
star = review.find("div", class_="star_score").text.strip()
reply = review.find("div", class_="score_reple")
comment = reply.find("p").text
date = reply.select("dt > em")[1].text.strip()
button = review.find("div", class_="btn_area")
sympathy = button.select("div > a > strong")
good = sympathy[0].text
bad = sympathy[1].text
review_list.append(Review(comment, date, star, good, bad))
return title, review_list
# 리뷰 정보 크롤링 실습
title, review_list = crawl("https://movie.naver.com/movie/bi/mi/basic.nhn?code=36944")
print('제목: ' + title)
for review in review_list:
review.show()
제목: 올드보이
내용:
이 영화는 필요 이상으로 너무 잘만들었다. 인간이 만든 작품이 아니다.
날짜: 2013.06.09 17:59
별점: 10
좋아요: 3711
싫어요: 222
내용:
충격적인 영화 촬영 기법, 스토리, 눈물샘을 자극시키는 사운드트랙. 대중영화 예술에 큰 기여를 한 혁명적인 영화.
날짜: 2013.06.09 01:08
별점: 10
좋아요: 2378
싫어요: 91
내용:
사람은 상상력이 있어서 비겁해 지는거래...
날짜: 2013.07.17 14:26
별점: 10
좋아요: 2118
싫어요: 74
내용:
10년만에 다시 본 올드보이. 역시 최고였다.
날짜: 2013.07.28 01:53
별점: 10
좋아요: 1610
싫어요: 75
내용:
개인적으로는 레옹보다 명작이라고 생각한다. 한국에서 다시 나오기 힘든 작품
날짜: 2013.07.15 11:15
별점: 10
좋아요: 1580
싫어요: 135
728x90
반응형
'Data Analysis > Data Analysis & Image Processing' 카테고리의 다른 글
19. 네이버 영화 리뷰 데이터 분석 2. 데이터 시각화 (0) | 2022.04.16 |
---|---|
17. 웹 크롤링 (0) | 2022.04.16 |
16. Matplotlib (0) | 2022.04.16 |
15. Pandas (0) | 2022.04.16 |
14. KNN 숫자 인식 예제 (0) | 2022.04.16 |
댓글