언어/python

python - 크롤링 - matplotlib - pie 차트 조합

파아랑새 2018. 10. 21. 16:20

# ---------------------------------------

from selenium import webdriver

from bs4 import BeautifulSoup

import re

import matplotlib.pyplot as plt

import pprint as ppr

import requests

from matplotlib import font_manager, rc

import os

# ---------------------------------------

driver = webdriver.Chrome('C:\\Users\\sleep\\Desktop\\chrom_driver\\chromedriver.exe')

driver.implicitly_wait(3)

driver.get("http://movie.naver.com/")

driver.find_element_by_xpath('//*[@id="BOXOFFICE_tab"]/a').click()

html = driver.page_source

bs_obj = BeautifulSoup(html, "html.parser")

title = bs_obj.find_all("div", {"class":"obj_off tab4"})

##flick1 > li.item2 > div.obj_off.tab4 > a > span.baseplate.r

##flick1 > li.item3 > div.obj_off.tab4 > a > span.baseplate.r > strong

#flick1 > li.item2 > div.obj_off.tab4 > a > span.baseplate.r > strong > span:nth-child(1)


m_info = dict()


for i in title:

    movieTitle = i.select_one("img")

    manCnt = i.select("a > span > strong > span > em")

    n = [t.string for t in manCnt]

    number = ''.join(n)

    number = float(re.sub('%', '', number))

    # print ("movie: {} -> {}".format(movieTitle.attrs['alt'], number))

    # print (movieTitle.attrs['alt'], manCnt)

    m_info[movieTitle.attrs['alt']] = number


# hex_color url

color_url = "https://www.color-hex.com/popular-colors.php"

html = requests.get(color_url)

c = BeautifulSoup(html.text, "html.parser")

c = c.find_all('div', {"class":"colordvcon"})

c = [i.select_one("a") for i in c]

c = [re.sub("[\n, \r, \t]", '', i.text) for i in c]

print (c)


# 폰트 지정 =====================================================

font_name = font_manager.FontProperties(fname='C:\\Windows\\Fonts\\malgunsl.ttf').get_name()

rc('font', family=font_name)

# if os.name == 'nt':

#     # os가 Windows인 경우 win32FontDirectory() 를 이용할 수 있다.

#     font_dir = font_manager.win32FontDirectory()

#     print (font_dir) # C:\Windows\Fonts

# font_path = os.path.join(font_dir, "consola.ttf")

# font = font_manager.FontProperties(fname=font_path, size=15)

# ==============================================================

# 그래프 크기 ====================================================

plt.figure(figsize=(80, 80))

labels = list(m_info.keys())

sizes  = list(m_info.values())

colors = c[:len(labels)]


plt.pie(sizes,

        labels=labels,

        colors=colors,

        autopct='%1.1f%%',

        shadow=True)


plt.show()