보호되어 있는 글입니다.
내용을 보시려면 비밀번호를 입력하세요.

확인

import pandas as pd
import matplotlib.pyplot as plt

class LineChart:


    def __init__(self):

        self._xlsx    = LineChart.getConfig()
        self._xvalues = list()
        self._yvalues = list()


    def doLine(self):

        for i in self._xlsx.index:
            rowData = self._xlsx["날짜"][i], self._xlsx["방문자수"][i]
            self._xvalues.append(rowData[0])
            self._yvalues.append(rowData[1])

        df = pd.DataFrame({"xvalues" : self._xvalues, "yvalues" : self._yvalues})

        # plot
        plt.plot("xvalues", "yvalues", data=df, color="g")
        plt.xlabel("year")
        plt.ylabel("count")
        plt.title("tistory my count")
        plt.show()


    @classmethod
    def getConfig(cls):

        PATH      = "/home/kim/Desktop/PY.dir/stu_01.dir/KimJH_uv.xlsx"
        SHEETNAME = "uv"
        xlxsObj = pd.read_excel(PATH, sheet_name=SHEETNAME)

        return xlxsObj

def main():

    obj = LineChart()
    obj.doLine()

if __name__ == "__main__":
    main()

'python > matplolib' 카테고리의 다른 글

matplotlib => pie_chart + xlsx  (0) 2019.11.02

import pandas as pd
import matplotlib.pyplot as plt

class PIE:

    def __init__(self):

        self._xlsx = PIE.getConfig()
        self._category = {"남자" : 0, "여자" : 0}

    def doPie(self):

        for i in self._xlsx.index:
            rowData = self._xlsx["성별"][i], self._xlsx["방문자수"][i]
            self._category[rowData[0]] += rowData[1]

        category         = ["man", "woman"]
        categorySize     = [self._category["남자"], self._category["여자"]]
        categoryColors   = ["yellow", "green"]
        categoryExplodes = (0.1, 0)

        plt.pie(categorySize,
                explode      = categoryExplodes,
                labels       = category,
                colors       = categoryColors,
                autopct      = '%1.2f%%',
                shadow       = True,
                startangle   = 90,
                textprops    = {"fontsize" : 14})  # text font size
        plt.axis("equal")
        plt.show()

    @classmethod
    def getConfig(cls):

        PATH      = "/home/kim/Desktop/PY.dir/stu_01.dir/KimJH.xlsx"
        SHEETNAME = "demographicsDashboard"
        xlxsObj = pd.read_excel(PATH, sheet_name=SHEETNAME)

        return xlxsObj

def main():

    obj = PIE()
    obj.doPie()

if __name__ == "__main__":
    main()

 

 

 

'python > matplolib' 카테고리의 다른 글

matplotlib => line_chart + excel  (0) 2019.11.02


from selenium import webdriver

from selenium.webdriver.chrome.options import Options

from selenium.webdriver.common.keys import Keys

import xlsxwriter    # 엑셀

from bs4 import BeautifulSoup

from urllib.request import urlretrieve

import time

import os

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

class Crawling(object):

    def __init__(self, search_data):

        # 작업 위치

        os.chdir(r'C:\Users\sleep\Desktop\date_info')

        self.search_data = search_data

        self.target_url = "https://www.naver.com/"

        self.chrome_options = Options() # 객체 생성

        self.chrome_options.add_argument("--headless")

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

                                       chrome_options=self.chrome_options)

        # 엑셀___________________________________________

        self.wb = None

        self.ws = None

        # ______________________________________________

        self.food_info_list = list()

        self.index_list = ['title', 'url', 'latitude','longitude']

        self.width_list = [87.5, 47.7, 15.8, 15.8]

        self.index_num = [2, 3, 4, 5]

    # func (1)

    def URLrequest(self):

        self.driver.get("https://www.naver.com/")

        assert "NAVER" in self.driver.title

        print ("title : {}".format(self.driver.title))

        self.driver.find_element_by_name('query').send_keys(self.search_data)

        self.driver.find_element_by_id('search_btn').click()

        # scroll bar

        self.driver.execute_script("window.scrollTo(0, document.body.scrollHeight/2);")

        self.driver.find_element_by_xpath('//*[@id="main_pack"]/div[2]/div[2]/a').click()

        page = 1

        x = 10

        while True:

            print ("{} 페이지 작업 중 ==========================".format(page))

            # scroll bar

            self.driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")

            html = BeautifulSoup(self.driver.page_source, "html.parser")

            time.sleep(3)

            li_list = html.select('ul.type01 > li') # list

            for i in li_list:

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

                t_data = {"thum_name_url":None,

                          "title":None,

                          "url":None,

                          "page":page}

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

                thumb_nail = i.select_one('div.review_thumb > a')

                try:

                    thumb_nail_url = thumb_nail.attrs['href']

                except AttributeError as e:

                    print (e)

                else:

                    t_data['thum_name_url'] = thumb_nail_url

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

                title_list = i.select_one('div.review_content > a')

                try:

                    title = title_list.attrs['title']

                except AttributeError as e:

                    print (e)

                else:

                    t_data['title'] = title

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

                try:

                    url = title_list.attrs['href']

                except AttributeError as e:

                    print (e)

                else:

                    t_data['url'] = url

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

                self.food_info_list.append(t_data)

                print (t_data)

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


            try:

                self.driver.find_element_by_xpath('//*[@id="main_pack"]/div[3]/a[{}]'.format(x)).click()

            except:

                print ("page _end")

                break

            else:

                x = 11

                page += 1

                time.sleep(1)


    # func (2)

    def XLwrite(self):

        self.wb = xlsxwriter.Workbook(self.search_data+'_.xlsx')

        self.cell_format = self.wb.add_format()

        self.cell_format.set_bottom()  # 테두리 : 바닥

        self.cell_format.set_top()  # 테두리 : 천장

        self.cell_format.set_left()  # 테두리 : 왼쪽

        self.cell_format.set_right()  # 테두리 : 오른쪽

        self.ws = self.wb.add_worksheet()

        # 셀 열너비

        for i ,w in zip(self.index_num, self.width_list):

            self.ws.set_column(i, i, w)


        r = 3

        c = 2

        for i in self.index_list:

            self.ws.write_string(row=r, col=c,

                                 string=i,

                                 cell_format=self.cell_format)

            c = c + 1

        r += 1 # 행

        c = 2

        for i in self.food_info_list:

            for j in i.keys():

                if j == 'title':

                    self.ws.write_string(row=r,col=c,

                                         string=i['title'],

                                         cell_format=self.cell_format)

                    c = c + 1

                elif j == 'url':

                    self.ws.write_string(row=r, col=c,

                                         string=i['url'],

                                         cell_format=self.cell_format)

                    c = c + 1

            c = 2

            r += 1  # 행

        self.wb.close()

        print ("엑셀 적재 완료")


def main():

    node = Crawling("여의도 데이트 음식점") # 객체 생성

    node.URLrequest()

    node.XLwrite()


if __name__ == "__main__":

    main()



'python > request + bs4' 카테고리의 다른 글

python3 request  (0) 2018.03.11

import requests

# HTML 소스 가져오기
def HTMLsouceGet(p):
print (p.text)

# HTTP 헤더 정보
def HTTPheaderGet(p):
retV = p.headers
return retV

def HTTPstatusGet(p):
print (p.status_code)

def main():
# GET방식으로 요청
req = requests.get(url='http://www.kitri.re.kr/academy/it_education/job_status2009.web')
is_ok = req.ok
print ("is_ok => {result}".format(result = is_ok))
if req.ok:
HTTPstatusGet(req)
d = HTTPheaderGet(req)
for i in d.keys():
print (i, d[i], sep='=> ')

else:
print ("요청 에러")

if __name__ == "__main__":
main()
 
is_ok => True
200
Date=> Sun, 11 Mar 2018 06:17:49 GMT
Server=> Apache/2.2.15 (CentOS)
Set-Cookie=> JSESSIONID=B27FC1FD587B1967B6F36A6A8755CA45; Path=/; HttpOnly
Content-Language=> ko-KR
Connection=> close
Transfer-Encoding=> chunked
Content-Type=> text/html;charset=UTF-8
 
 

 


 

'python > request + bs4' 카테고리의 다른 글

데이트를 위해 맛집 크롤링 중  (0) 2018.11.22

python numpy 01

python/numpy2018. 2. 18. 18:17

"""

Numerical Python

고성능의 과학계산 컴퓨팅과 데이터 분석에 필요한 기본 패키지


제공하는 기능

1) 빠르고 메모리를 효율적으로 사용하며 벡터 산술연산과 세련된

브로드캐스팅 기능을 제공하는 다차원 배열인 ndarray

2) 반복문을 작성할 필요없이 전체 데이터 배열에 대해 빠른 연산을

제공하는 표준 수학 함수

3) 배열 데이터를 디스크에 쓰거나 읽을 수 있는 도구와 메모리에 올려진

파일을 사용하는 도구

4) 선형대수(linear algebra), 난수 발생기, 푸리에 변환 기능

5) c, c++, 포트란으로 쓰여진 코드를 통합하는 도구

"""

import numpy as np

data = np.random.randn(3, 3)

print (data)


[[ 0.07353147  0.2299053  -1.40381414]

 [ 0.28591902 -0.13944514 -0.2153317 ]

 [-0.82442217 -0.94539717  0.30150343]]


random.randn 함수는 Return a sample (or samples) from the standard normal” distribution. (표준 정규 분포)


# 벡터의 모양 출력

print (data.shape) 

=> data 가 3x3 행렬 이기 때문에 (3,3) return


# 벡터의 원소들 데이터 타입 출력

print (data.dtype)

=> float64


# 차원

print (data.ndim)

=> 2차원 



# ndarray 생성

import numpy as np

import pprint as ppr

s = [n for n in range(3, 20)]

vec = np.array(s)

ppr.pprint (vec)


# 자료형 

s = [n for n in range(3, 20)] # 요 녀석은 파이썬의 list 타입

vec = np.array(s, dtype=np.float32)


# 새로운 배열을 생성

[1] zeros

import numpy as np

import pprint as ppr

np = np.zeros(shape=(4, 4))

ppr.pprint(np)

=>

array([[0., 0., 0., 0.],

       [0., 0., 0., 0.],

       [0., 0., 0., 0.],

       [0., 0., 0., 0.]])

[2] arange

import numpy as np

v = np.arange(start=10, stop=30, step=1, dtype=np.int16)

print (v)

=>

[10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29]