<jupyter_start><jupyter_text>네이버 영화 평점 크롤링<jupyter_code>from bs4 import BeautifulSoup
from urllib.request import urlopen

url = 'https://movie.naver.com/movie/sdb/rank/rmovie.nhn?sel=cur&date=20200402'
page = urlopen(url)

soup = BeautifulSoup(page, 'html.parser')
soup.find_all('div', 'tit5')[:3]
soup.find_all('div', 'tit5')[0].a
soup.find_all('div', 'tit5')[0].a.string
movie_name = [each.a.string for each in soup.find_all('div', 'tit5')]
movie_name[:3]
soup.find_all('td', 'point')[0].string
movie_point = [each.string for each in soup.find_all('td', 'point')]
movie_point[:3]
date = pd.date_range('2020.03.01', periods=31, freq='D')
date
date[0]
date[0].strftime('%y-%m-%d')
date[0].strftime('%y.%m.%d')
date[0].strftime('%y%m%d')
date[0].strftime('%Y%m%d')
movie_date = []
movie_name = []
movie_point = []
for today in date:
    html = 'https://movie.naver.com/movie/sdb/rank/rmovie.nhn?sel=cur&date={date}'
    response = urlopen(html.format(date=today.strftime('%Y%m%d')))
    soup = BeautifulSoup(response, 'html.parser')
    
    movie_date.extend([today]*len(soup.find_all('td', 'point')))
    movie_name.extend([each.a.string for each in soup.find_all('div', 'tit5')])        
    movie_point.extend([each.string for each in soup.find_all('td', 'point')])    

    time.sleep(0.5)
len(movie_date), len(movie_name),  len(movie_point)
movie = pd.DataFrame({'date': movie_date, 'name': movie_name, 'point': movie_point})
movie.head(3)
movie.info()
movie['point'] = movie['point'].astype(float)
movie.info()
movie.to_csv('./naver_movie_raw_data.csv', sep=',', encoding='utf-8')
movie = pd.read_csv('./naver_movie_raw_data.csv', index_col=0)
movie.head(3)<jupyter_output><empty_output><jupyter_text>영화 이름으로 인덱스를 잡고 점수의 합산<jupyter_code>movie_unique = pd.pivot_table(movie, index=['name'], aggfunc=np.sum)
movie_unique.head(3)<jupyter_output><empty_output><jupyter_text>한달 간 네이터 영화 평점 합산 기준 베스트 10<jupyter_code>movie_best = movie_unique.sort_values(by='point', ascending=False)
movie_best.tail(10)<jupyter_output><empty_output><jupyter_text>DataFrame의 검색 명령 : query<jupyter_code>movie.query('name == ["<NAME>"]').head(3)<jupyter_output><empty_output><jupyter_text>한 영화에 대한 날짜별 점수<jupyter_code>%matplotlib inline

plt.figure(figsize=(12, 8))
plt.plot(movie.query('name == ["벌새"]')['date'],
        movie.query('name == ["벌새"]')['point'])
plt.legend(loc='best')
plt.grid()
plt.grid(); plt.show()
movie_best.head(3)
movie_best.tail(3)<jupyter_output><empty_output><jupyter_text>한 달 간의 영화를 모두 정리<jupyter_code>movie_pivot = pd.pivot_table(movie, index=['date'], columns=['name'])
movie_pivot.columns = movie_pivot.columns.droplevel([0])
movie_pivot.head()<jupyter_output><empty_output><jupyter_text>엑셀로 저장<jupyter_code>movie_pivot.to_excel('./movie_pivot.xlsx')
movie_pivot.columns<jupyter_output><empty_output><jupyter_text>보고싶은 영화 몇개만 추려서 그래프로 확인<jupyter_code>target_col = ['1917', '500일의 썸머', '겨울왕국 2', '기생충', '포<NAME>']

plt.figure(figsize=(12, 8))
plt.plot(movie_pivot[target_col])
plt.legend(target_col, loc='best')
plt.tick_params(bottom=False, labelbottom=False)
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1928020565552699, 'fraction_numerical': 0.02056555269922879, 'mean_word_length': 7.459239130434782, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 35, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '268297', 'license': 'MIT', 'n_tokens_mistral': 1461, 'n_tokens_neox': 1451, 'n_words': 177, 'path': 'web_crawling/0403_crawling_naver_movie.ipynb', 'repo_name': 'pearl-lee/TIL', 'size': '3112'}