https://movie.naver.com/movie/sdb/rank/rmovie.naver?sel=cur&date=20210914
import pandas as pd
from urllib.request import urlopen
from bs4 import BeautifulSoup
url = "https://movie.naver.com/movie/sdb/rank/rmovie.naver?sel=cur&date=20210914"
response = urlopen(url)
soup = BeautifulSoup(response, "html.parser")
print(soup.prettify())
soup.find_all("div", "tit5") # soup.select("div.tit5")
soup.find_all("div", "tit5")[0].a.string
soup.select(".tit5")[0].find("a").text
soup.select(".tit5")[0].select_one("a").get_text()
soup.findall("td", "point") # soup.select(".point")
len(soup.find_all("td", "point")), len(soup.find_all("div", "tit5"))
soup.find_all("td", class="point")[0].text, soup.select("td.point")[0].string
end = len(soup.find_all("div", "tit5"))
movie_name = []
for n in range(0, end):
movie_name.append(
soup.find_all("div", "tit5")[n].a.string
)
movie_name
movie_name = [soup.select(".tit5")[n].a.text for n in range(0, end)]
movie_name
end = len(soup.find_all("td", "point"))
movie_point = [soup.find_all("td", "point")[n].string for n in range(0, end)]
movie_point
len(movie_name), len(movie_point)
https://movie.naver.com/movie/sdb/rank/rmovie.naver?sel=cur&date=20210914
test_string = "Hi, I'm {name}"
test_string.format(name="Zerobase")
test_string.format(name="Pinkwink")
import time
from tqdm import tqdm
movie_date = []
movie_name = []
movie_point = []
for today in tqdm(date):
url = "https://movie.naver.com/movie/sdb/rank/rmovie.naver?sel=cur&date={date}"
response = urlopen(url.format(date=today.strftime("%Y%m%d")))
soup = BeautifulSoup(response, "html.parser")
end = len(soup.find_all("td", "point"))
movie_date.extend([today for _ in range(0, end)])
movie_name.extend([soup.select("div.tit5")[n].find("a").get_text() for n in range(0, end)])
movie_point.extend([soup.find_all("td", "point")[n].string for n in range(0, end)])
time.sleep(0.5)
len(movie_date), len(movie_name), len(movie_point)
movie_point[:5]
movie_name[:5]
movie = pd.DataFrame({
"date": movie_date,
"name": movie_name,
"point": movie_point
})
movie.tail()
movie.info()
movie["point"] = movie["point"].astype(float)
movie.info()
movie.to_csv(
"../data/03. naver_movie_data.csv", sep=",", encoding="utf-8"
)
import numpy as np
import pandas as pd
movie = pd.read_csv("../data/03. naver_movie_data.csv", index_col=0)
movie.tail()
movie_unique = pd.pivot_table(data=movie, index="name", aggfunc=np.sum)
movie_unique
movie_best = movie_unique.sort_values(by="point", ascending=False) # 내림차순
movie_best.head()
tmp = movie.query("name == ['화양연화']")
tmp
import matplotlib.pyplot as plt
from matplotlib import rc
rc("font", family="Malgun Gothic") # Windows: Malgun Gothic / Mac: Arial Unicode MS
%matplotlib inline
plt.figure(figsize=(20, 8)) # x 20, y, 8
plt.plot(tmp["date"], tmp["point"]) # 선 그래프 x축 날짜, y축 평점 => 날짜에 따른 평점 변화를 선그래프로 표현(시계열)
plt.title("날짜별 평점")
plt.xlabel("날짜")
plt.ylabel("평점")
plt.xticks(rotation="vertical")
plt.legend(labels=["평점 추이"], loc="best")
plt.grid(True)
plt.show()
movie_best.head(10)
movie_best.tail(10)
movie_pivot = pd.pivot_table(data=movie, index="date", columns="name", values="point")
movie_pivot.head()
movie_pivot.to_excel("../data/03. movie_pivot.xlsx")
import platform
import seaborn as sns
from matplotlib import font_manager, rc
path = "C:/Windows/Fonts/malgun.ttf"
if platform.system() == "Darwin":
rc("font", family="Arial Unicode MS")
elif platform.system() == "Windows":
font_name = font_manager.FontProperties(fname=path).get_name()
rc("font", family=font_name)
else:
print("Unknown system. sorry")
target_col = ["화양연화", "국가부도의 날", "하나와 앨리스", "늑대와 춤을", "피아노"]
plt.figure(figsize=(20, 8))
plt.title("날짜별 평점")
plt.xlabel("날짜")
plt.ylabel("평점")
plt.xticks(rotation="vertical")
plt.tick_params(bottom="off", labelbottom="off")
plt.plot(movie_pivot[target_col])
plt.legend(target_col, loc="best")
plt.grid(True)
plt.show()