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

import selenium.webdriver as selWeb

import time

import cv2

from urllib.request import urlretrieve

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

class STU:

    # 생성자

    def __init__(self):

        # 인스턴스 변수

        self.webDriver = selWeb.Chrome("C:\\Users\\sleep\\Desktop\\chrom_driver\\chromedriver.exe")


    # 인스턴스 메서드

    def func01(self, searchData):

        self.webDriver.get("https://www.google.co.kr/")

        time.sleep(2) # 2+

        self.webDriver.find_element_by_name('q').send_keys(searchData)

        time.sleep(2)

        self.webDriver.find_element_by_css_selector('input.lsb').click()

        time.sleep(2)

        # self.webDriver.find_element_by_xpath('//*[@id="hdtb-msb-vis"]/div[2]/a').click()

        btn = self.webDriver.find_elements_by_css_selector('#hdtb-msb-vis > div > a')

        btn[1].click()

        # //*[@id="hdtb-msb-vis"]/div[2]/a

        time.sleep(2)

        img = self.webDriver.find_element_by_xpath('//*[@id="AiydHE6wkRVBlM:"]')

        src = img.get_attribute('src')


        urlretrieve(src, "mit.jpg")


    def cv2ImageView(self):

        self.webDriver.close()

        img = cv2.imread('mit.jpg', 0)

        cv2.imshow('mit', img)

        cv2.waitKey(0)

        cv2.destroyAllWindows()


def main():

    sNode = STU() # 객체 생성

    sNode.func01("MIT")

    sNode.cv2ImageView()

if __name__ == "__main__":

    main()



'언어 > python' 카테고리의 다른 글

카라바오 컵 파이썬 크롤링  (0) 2018.11.01
맛집 크롤링  (0) 2018.10.31
pygame _ 연습2  (0) 2018.10.27
pygame _ 연습중  (0) 2018.10.27
selenium + 자동화 + 네이버 메일 관리 부분 version 0.1  (0) 2018.10.27

pygame _ 연습2

언어/python2018. 10. 27. 13:29

[+] MOUSEMOTION


import pygame as pg


pg.init()


# 화면 셋팅

out_screen = pg.display.set_mode((640, 400))

clock = pg.time.Clock()

dead_signal = False

bg_color = 0, 0, 0 # type of tuple

while not dead_signal:

    for e in pg.event.get():

        if e.type == pg.QUIT:

            dead_signal = True

        elif e.type == pg.MOUSEMOTION:

            xPos, yPos = e.pos

    pg.draw.line(out_screen,(255,255,255), (30, 250), (xPos, yPos))

    pg.display.flip()

    clock.tick(2)




'언어 > python' 카테고리의 다른 글

맛집 크롤링  (0) 2018.10.31
selenium - 테스트 코드  (0) 2018.10.30
pygame _ 연습중  (0) 2018.10.27
selenium + 자동화 + 네이버 메일 관리 부분 version 0.1  (0) 2018.10.27
python - selenium + urllib  (0) 2018.10.26

pygame _ 연습중

언어/python2018. 10. 27. 13:09

import pygame as pg


pg.init()

# 화면

out_screen = pg.display.set_mode((840, 840))

clock = pg.time.Clock()

dead_signal = False

""" [x 축] """

posX = 0x0

""" [y 축] """

posY = 0x0

while not dead_signal:

    for e in pg.event.get():

        if e.type == pg.QUIT:

            dead_signal = True

        elif e.type == pg.MOUSEMOTION:

            posX, posY = e.pos

            print ("mouse at (%d, %d)"%e.pos)

            if 250 <= posX <= 400 and \

                250 <= posY <= 400:

                print ("이벤트 발생")

                pg.draw.rect(out_screen,(0, 255, 128), (posX, posY, 50, 50))

                pg.display.update()

            else:

                out_screen.fill((0, 0, 0))

    # out_screen.fill((0, 0, 0))

    pg.display.flip()

    clock.tick(2)



'언어 > python' 카테고리의 다른 글

selenium - 테스트 코드  (0) 2018.10.30
pygame _ 연습2  (0) 2018.10.27
selenium + 자동화 + 네이버 메일 관리 부분 version 0.1  (0) 2018.10.27
python - selenium + urllib  (0) 2018.10.26
pygrame01  (0) 2018.10.24

import Database

import selenium.webdriver as selWeb

import time

from bs4 import BeautifulSoup

import re

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

class Center(Database.DB):

    def __init__(self, url): # 생성자

        Database.DB.__init__(self)

        self.targetURL = url


        # selenium

        self.webDriver = selWeb.Chrome('C:\\Users\\sleep\\Desktop\\chrom_driver\\chromedriver.exe')


        # naver_voice_number

        self.naverVoiceNumber = ""


        # filter

        self.textFilter = re.compile("^새로운 기기\(브라우저\)에서 로그인 되었습니다.")

    # func 01

    def urlRequset_center(self):

        self.webDriver.get(self.targetURL)

        time.sleep(2) # 지연

        self.webDriver.find_element_by_xpath('//*[@id="account"]/div/a/i').click()

        time.sleep(2)  # 지연

        #ID / PW 값 넣기

        self.webDriver.find_element_by_id('id').send_keys(self.usrInfo['id'])

        self.webDriver.find_element_by_id('pw').send_keys(self.usrInfo['pw'])

        self.webDriver.find_element_by_xpath('//*[@id="frmNIDLogin"]/fieldset/input').click()

        time.sleep(2)  # 지연

        try:

            # capcha 부분 자동 로그인 방지

            self.webDriver.find_element_by_xpath('//*[@id="image_captcha"]/a[2]/span').click()

            while not self.naverVoiceNumber:

                self.naverVoiceNumber = input("number input :  ")


            self.webDriver.find_element_by_id('chptcha').send_keys(self.naverVoiceNumber)

            self.webDriver.find_element_by_id('pw').send_keys(self.usrInfo['pw'])

            self.webDriver.find_element_by_xpath('//*[@id="login_submit"]').click()

        except:

            # 등록/ 등록안함

            self.webDriver.find_element_by_xpath('//*[@id="frmNIDLogin"]/fieldset/span[2]/a').click()

        else:

            # 등록/ 등록안함

            self.webDriver.find_element_by_xpath('//*[@id="frmNIDLogin"]/fieldset/span[2]/a').click()

        time.sleep(2)

        # 메일 클릭

        #  selenium이 span 태그 제어 못함 그래서 find_element_by_link_text를 사용함

        self.webDriver.find_element_by_link_text("메일").click()

        # self.webDriver.find_elements_by_xpath('//*[@id="PM_ID_ct"]/div[1]/div[2]/div[1]/ul[1]/li[1]/a/span[2]').click()

        time.sleep(2)

        # 페이지 클릭


        #

        # //*[@id="normalPagingNav"]/span[1]/strong

        # for p in range(2, 11):

        #     tmpHtml = self.webDriver.page_source

        #     tmpObject = BeautifulSoup(tmpHtml, 'html.parser')

        #     t = tmpObject.find_all('ul',{"class":"mInfo"})


        for p in range(2, 11):

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

            tmpHtml = self.webDriver.page_source

            tmpObject = BeautifulSoup(tmpHtml, "html.parser")

            # t = tmpObject.find_all('div', {'class':'mTitle'})

            t = tmpObject.find_all('strong', {'class':re.compile("^mail_title")})

            cunt = 0x0

            for i in t:

                if self.textFilter.search(i.text[6:]):

                    cunt += 1

                    print(cunt, " : ",i.text[6:], " : ", i.attrs['mailsn'])

                    self.webDriver.find_element_by_xpath('//*[@id="mailCheck_{}"]'.format(i.attrs['mailsn'])).click()

                    time.sleep(2)

                    # 이메일 삭제

                    self.webDriver.find_element_by_xpath('//*[@id="listBtnMenu"]/div[1]/span[2]/button[2]').click()

                    time.sleep(2)

                #     //*[@id="mailCheck_19085"]

                # tempEmail = ""

                # j = i.find("div", {'class':'name _ccr(lst.from) '})

                # tempEmail += j.text

                #

                # j = i.find("div", {'class':'subject'})

                # tempEmail += j.text

                # print (tempEmail)


                # if self.textFilter.findall(tempEmail):

                #     checkBox = tmpObject.find_all('ul', {"class": "mInfo"})

                #     tempV = [i.select_one('input') for i in checkBox]

                #     print ("=========> {}".format(tempV))


            self.webDriver.find_element_by_link_text("{}".format(p)).click()

            time.sleep(4)


def main():

    sNode = Center("https://www.naver.com/")

    sNode.urlRequset_center()

if __name__ == "__main__":

    main()




'언어 > python' 카테고리의 다른 글

pygame _ 연습2  (0) 2018.10.27
pygame _ 연습중  (0) 2018.10.27
python - selenium + urllib  (0) 2018.10.26
pygrame01  (0) 2018.10.24
selenium + 잡코리아 크롤링 + openpyxl  (0) 2018.10.24

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

import selenium.webdriver as selWeb

import urllib.request as urlReq

import time

from bs4 import BeautifulSoup

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

class STU01(object):

    # 생성자

    def __init__(self, url):

        # Instance variable

        self.webDriver = selWeb.Chrome(

            'C:\\Users\\sleep\\Desktop\\chrom_driver\\chromedriver.exe'

        )

        self.targetUrl = url

        self.html = None


        # BeautifulSoup

        self.bsObject = None


        # save img_file

        self.saveImg = "harry_potter.jpg"

    """

    영화 : #PM_ID_themelist > ul > li:nth-child(12) > a

    방향 이동 : //*[@id="PM_ID_themecastNavi"]/div[2]/a[2]

    """

    # FUNC (1)

    def reqURL(self):

        self.webDriver.get(self.targetUrl)

        time.sleep(4)

        while True:

            try:

                self.webDriver.find_element_by_xpath('//*[@id="PM_ID_themelist"]/ul/li[12]/a').click()

                time.sleep(2)

            except:

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

                time.sleep(4)

            else:

                break

        self.html = self.webDriver.page_source

        self.bsObject = BeautifulSoup(self.html, "html.parser")

        img_data = self.bsObject.select_one('#PM_ID_themecastBody > div > div > div > ul > li.tl_bigimage > a > div.tb_mw > img')

        urlReq.urlretrieve(img_data.attrs['src'], self.saveImg)

        print ("Image save success ...")


def main():

    stCrawling = STU01("https://www.naver.com/")

    stCrawling.reqURL()

if __name__ == "__main__":

    main()

pygrame01

언어/python2018. 10. 24. 21:45

import pygame as pg

import time

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

# 초기화

pg.init()


# 화면

our_screen = pg.display.set_mode((540, 540))

pg.display.set_caption("병아리")


finish = False

color = (255, 0, 0) # RGB (Red, Green, Blue)

data = {'x':0, 'y':0}

while not finish:

    for e in pg.event.get():

        if e.type == pg.QUIT: # 종료 이벤트

            finish = True

        else: # e.type != pg.QUIT

            if e.type == pg.KEYDOWN:

                if e.key == pg.K_SPACE:

                    if color == (255, 0, 0): color = (0, 255, 0)

                    elif color == (0, 255, 0): color = (255, 0, 0)

                else: # 이동 발생

                    pg.draw.rect(our_screen, (0, 0, 0), pg.Rect(data['x'], data['y'], 60, 60))

                    if e.key == pg.K_LEFT: # 왼쪽으로 이동

                        if data['x'] != 0:

                            data['x'] -= 10

                    elif e.key == pg.K_RIGHT: # 오른쪽으로 이동

                        if data['x'] != 540:

                            data['x'] += 10

                    elif e.key == pg.K_UP: # 위로 이동

                        if data['y'] != 0:

                            data['y'] -= 10

                    elif e.key == pg.K_DOWN: # 아래로 이동

                        if data['y'] != 540:

                            data['y'] += 10

    pg.draw.rect(our_screen, color, pg.Rect(data['x'], data['y'], 60, 60)) # 사각형

    pg.display.flip() # 화면 update

from selenium import webdriver

import time

from bs4 import BeautifulSoup

from tinydb import TinyDB

from openpyxl import Workbook, load_workbook

import re

import pprint as ppr

from openpyxl.styles import PatternFill, Color

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

"""

//*[@id="smGiList"]/div[3]/ul/li[1]/span

//*[@id="smGiList"]/div[3]/ul/li[2]/span

"""

class JobKorea:

    def __init__(self, s):

        # instance 변수 셋팅

        # Chrome driver open

        self.browser = webdriver.Chrome("C:\\Users\\sleep\\Desktop\\getDriveFirfox\\chromedriver.exe")

        self.searchData = s

        self.Data = []

        self.pageValue = 0x0


        # re-

        self.filter_todayEnd = re.compile(pattern='오늘마감')

        self.filter_always = re.compile(pattern='상시모집')

        self.filter_date = re.compile('(\d{1,2})/(\d{1,2})')

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


        # excel-

        self.wb = Workbook() # 객체 생성, workBook

        self.ws = None # workSheet

        self.loadWkbook = None

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

        self.execel_dict = {} # type of dictionary


    # instance method - 1

    def URLrequest(self):

        self.browser.get("http://www.jobkorea.co.kr/Search/")

        # 'Python' 검색

        self.browser.find_element_by_id('stext').send_keys(self.searchData)

        self.browser.find_element_by_xpath('//*[@id="common_search_btn"]').click()


        # 조건 : 근무 경력 - 신입

        self.browser.find_element_by_xpath('//*[@id="search_wrap"]/dl[5]/dd/div[1]/ul/li[1]/label/span/span').click()


        # 시간 : 3초 - 반드시 줄 것 아니면 값이 제대로 들어가지 않음

        time.sleep(3)

        self.PageURLrequest()


    # instance method - 2

    def PageURLrequest(self):

        p = 1

        # page

        while True:

            try:

                self.browser.find_element_by_xpath('//*[@id="smGiList"]/div[3]/ul/li[{}]'.format(p)).click()

            except:

                self.pageValue = p - 1

                break

            else: # 성공

                # 시간 : 3초 - 반드시 줄 것 아니면 값이 제대로 들어가지 않음

                time.sleep(3)

                html = self.browser.page_source  # HTML.text

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

                title = bs_object.find_all('span', {'class': 'corpName'})

                infov = bs_object.find_all('span', {'class': 'detailInfo'})

                print("{}:page =================".format(p))

                for i, t in enumerate(zip(title, infov)):

                    unit_data = {} # dictionary data


                    name = t[0].select_one('a')

                    tinf = t[1].select_one('p.gibInfo > a')

                    tmpv = t[1].select_one('p.gibDesc > a > em')

                    tinf = re.sub('[\n\r\t ]', '', tinf.text) # \n과 \r, \t, , , 를 제거한다.

                    tinf = tinf.split(',') # list 화


                    # name : 회사 이름

                    print("{0:02d} => {1:s}, {2}".format(i + 1, name.string, tinf))

                    unit_data[name.string] = {'info':tinf,

                                              'date':None,

                                              'page':self.searchData + '_' + str(p)}



                    if str(tmpv.string).startswith('~'):

                        t = self.filter_date.search(str(tmpv.string)[1:])

                        tIndx = t.span()

                        print (str(tmpv.string)[1:][tIndx[0]:tIndx[1]])

                        unit_data[name.string]['date'] = str(tmpv.string)[1:][tIndx[0]:tIndx[1]]

                    else:

                        # 오늘 마감

                        if self.filter_todayEnd.search(str(tmpv.string)):

                            print ("오늘마감")

                            unit_data[name.string]['date'] = '오늘마감'

                        # 상시 모집

                        elif self.filter_always.search(str(tmpv.string)):

                            print ("상시모집")

                            unit_data[name.string]['date'] = '상시모집'

                        else:

                            print (str(tmpv.string))

                            unit_data[name.string]['date'] = str(tmpv.string)

                    self.Data.append(unit_data)

                print("========================")

                p = p+1 # page up


    # instance method - 3 sheet 생성

    def createSheet(self):

        for i in range(1, self.pageValue+1):

            self.wb.create_sheet(self.searchData + '_' + str(i))

        self.wb.save('C:\\Users\\sleep\\Desktop\\jobKorea_{}.xlsx'.format(self.searchData))

        print ("save success ...")


    # instance method - 4

    def excelPageSetting(self):

        # 엑셀 _ controller

        for p in range(1, self.pageValue+1):

            self.execel_dict[self.searchData + '_' + str(p)] = \

                        {"회사이름": [ord('B'), 3],

                         "정보": [ord('B'), 4],

                         "날짜": [ord('B'), 5],

                         "번호": [ord('A'), 3],

                         "count": 1}


    # instance method - 5 : loadWorkbook

    def xlsxWrite(self):

        # 엑셀 파일 load

        self.loadWkbook = load_workbook('C:\\Users\\sleep\\Desktop\\jobKorea_{}.xlsx'.format(self.searchData))

        # self.loadWkbook.sheetnames # ['Sheet', 'python_1', 'python_2', 'python_3', 'python_4', 'python_5', 'python_6']

        for i in range(0, len(self.Data)):

            for k, v in self.Data[i].items():

                tempJump = 0x0

                # 엑셀 내용 저장

                WkSheet = self.loadWkbook[v['page']] # worksheet load

                # 번호) ---------------------------------------------

                inxCha = self.execel_dict[v['page']]['번호'][0]

                inxInt = self.execel_dict[v['page']]['번호'][1]

                WkSheet[chr(inxCha) + str(inxInt)] = self.execel_dict[v['page']]['count']


                # # 회사 이름) ---------------------------------------

                inxCha = self.execel_dict[v['page']]['회사이름'][0]

                inxInt = self.execel_dict[v['page']]['회사이름'][1]

                WkSheet[chr(inxCha) + str(inxInt)] = "회사이름" # - 회사이름

                # 셀 너비 조정

                # WkSheet.column_dimensions[chr(inxCha) + str(inxInt)].width = len("회사이름")

                inxCha = inxCha + 1

                WkSheet[chr(inxCha) + str(inxInt)] = k # - 구글


                # 요구 기술) -----------------------------------------

                inxCha = self.execel_dict[v['page']]['정보'][0] # "B"

                inxInt = self.execel_dict[v['page']]['정보'][1] #  4

                WkSheet[chr(inxCha) + str(inxInt)] = "정보"  # - 정보

                tmpWidth = 0x0

                inxCha = inxCha + 1

                for j in range(0, len(v['info'])):

                    if inxCha > ord('Z'): # 값이 넘어 가는 경우

                        inxCha = ord('C')

                        tempJump += 1

                        inxInt += 1

                    WkSheet[chr(inxCha) + str(inxInt)] = v['info'][j]

                    # 셀 병합 ==========================================

                    front = chr(inxCha) + str(inxInt)

                    if inxCha+1 > ord('Z'):

                        rear = 'AA' + str(inxInt)

                    else: # inxCha+1 <= ord('Z')

                        rear = chr(inxCha+1) + str(inxInt)

                    WkSheet.merge_cells(front+':'+rear)

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

                    inxCha = inxCha + 2

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

                    # 열 너비 조정

                    # if len(v['info'][j]) > tmpWidth:

                    #     WkSheet.column_dimensions[chr(inxCha)].width = len(v['info'][j])

                    #     tmpWidth = len(v['info'][j])


                    # 셀 너비 조정

                    # WkSheet.column_dimensions[chr(inxCha) + str(inxInt)].width = len(v['info'][j])


                if self.execel_dict[v['page']]['정보'][1] != inxInt:

                    self.execel_dict[v['page']]['날짜'][1] = inxInt + 1

                # 날짜) ---------------------------------------------

                inxCha = self.execel_dict[v['page']]['날짜'][0]

                inxInt = self.execel_dict[v['page']]['날짜'][1]

                WkSheet[chr(inxCha) + str(inxInt)] = "마감날짜"

                inxCha = inxCha + 1

                WkSheet[chr(inxCha) + str(inxInt)] = v['date']

                if v['date'] == "오늘마감":

                    WkSheet[chr(inxCha) + str(inxInt)].fill = \

                        PatternFill(patternType='solid', fgColor=Color('ff547d'))

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

                # 4씩 값을 증가

                self.execel_dict[v['page']]['번호'][1] += (4 + tempJump)

                self.execel_dict[v['page']]['회사이름'][1] += (4 + tempJump)

                self.execel_dict[v['page']]['정보'][1] += (4 + tempJump)

                self.execel_dict[v['page']]['날짜'][1] += 4

                self.execel_dict[v['page']]['count'] += 1


                # self.loadWkbook.save('C:\\Users\\sleep\\Desktop\\jobKorea_{}.xlsx'.format(self.searchData))

                # exit(1)

        self.loadWkbook.save('C:\\Users\\sleep\\Desktop\\jobKorea_{}.xlsx'.format(self.searchData))

    # 소멸자 -

    # def __del__(self):

    #     self.wb.save('C:\\Users\\sleep\\Desktop\\jobKorea_{}.xlsx'.format(self.searchData))


def main():

    jobNode = JobKorea('python') # 객체 생성

    jobNode.URLrequest() # instance method - 1

    jobNode.createSheet() # instance method - 3

    jobNode.excelPageSetting() # instance method - 4

    jobNode.xlsxWrite() # instance method - 5

    # ppr.pprint (jobNode.Data)

if __name__ == "__main__":

    main()








'언어 > python' 카테고리의 다른 글

python - selenium + urllib  (0) 2018.10.26
pygrame01  (0) 2018.10.24
python - 크롤링 - matplotlib - pie 차트 조합  (0) 2018.10.21
네이버 영화 크롤링 파이썬  (0) 2018.10.20
python watchdog 공부  (0) 2018.10.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()



'언어 > python' 카테고리의 다른 글

pygrame01  (0) 2018.10.24
selenium + 잡코리아 크롤링 + openpyxl  (0) 2018.10.24
네이버 영화 크롤링 파이썬  (0) 2018.10.20
python watchdog 공부  (0) 2018.10.20
정규식  (0) 2018.10.16

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

import requests

from bs4 import BeautifulSoup

import pprint as ppr

from datetime import date

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

'''

https://movie.naver.com/movie/sdb/rank/rmovie.nhn?sel=cnt&date=20181019

https://movie.naver.com/movie/sdb/rank/rmovie.nhn?sel=cnt&date=20181019&tg=18

https://movie.naver.com/movie/sdb/rank/rmovie.nhn?sel=cnt&tg=18&date=20181017

#old_content > table > tbody > tr:nth-child(1)

'''

target_url = "https://movie.naver.com/movie/sdb/rank/rmovie.nhn?"

params = {

    "sel":"cnt",

    "date":"20181019",

    "tg":"18",

}


print (date.today())

today_information = date.today().strftime('%Y-%m-%d')

html = requests.get(target_url)

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

value = [x for x in bs_obj.select("tbody > tr")]


insert_data = {today_information:None}

temp_info = dict() # type of dictionary

for v in value:

    rank, title = v.find("td", {"class":"ac"}), v.find("td", {"class":"title"})

    if rank and title:

        r = "{0:02d}".format(int(rank.select_one("img").attrs['alt'])) # 랭킹

        t = "{0:s}".format(title.select_one("div > a").attrs['title']) # 영화이름

        v = "{} - {}".format(r,t)

        temp_info[r] = t


insert_data[today_information] = temp_info

ppr.pprint (insert_data)




===========================================================================

2018-10-20

{'2018-10-20': {'01': '퍼스트맨',

                '02': '미쓰백',

                '03': '암수살인',

                '04': '베놈',

                '05': '스타 이즈 본',

                '06': '박화영',

                '07': '안시성',

                '08': '명당',

                '09': '서치',

                '10': '원더풀 고스트',

                '11': '창궐',

                '12': '협상',

                '13': '배반의 장미',

                '14': '곰돌이 푸 다시 만나 행복해',

                '15': '액슬',

                '16': '물괴',

                '17': '여곡성',

                '18': '82년생 김지영',

                '19': '완벽한 타인',

                '20': '그랜드 부다페스트 호텔',

                '21': '펭귄 하이웨이',

                '22': '킨: 더 비기닝',

                '23': '마라',

                '24': '국가부도의 날',

                '25': '핫 썸머 나이츠',

                '26': '노크: 초대받지 않은 손님',

                '27': '호밀밭의 반항아',

                '28': '나를 차버린 스파이',

                '29': '배드 사마리안',

                '30': '신비한 동물들과 그린델왈드의 범죄',

                '31': '상류사회',

                '32': '다이노 어드벤처2: 육해공 공룡 대백과',

                '33': '업그레이드',

                '34': '에브리데이',

                '35': '리즈와 파랑새',

                '36': '보헤미안 랩소디',

                '37': '할로윈',

                '38': '그놈이다',

                '39': '동네사람들',

                '40': '인피니티 포스 : 독수리오형제 최후의 심판',

                '41': '성난황소',

                '42': '무뢰한',

                '43': '너의 결혼식',

                '44': '맥퀸',

                '45': '타샤 튜더',

                '46': '블랙 47',

                '47': '해리 포터와 마법사의 돌',

                '48': '크레이지 리치 아시안',

                '49': '양의 나무',

                '50': '극장판 가면라이더 이그제이드: 트루 엔딩'}}



'언어 > python' 카테고리의 다른 글

selenium + 잡코리아 크롤링 + openpyxl  (0) 2018.10.24
python - 크롤링 - matplotlib - pie 차트 조합  (0) 2018.10.21
python watchdog 공부  (0) 2018.10.20
정규식  (0) 2018.10.16
virusshare malware hash crawling python  (0) 2018.10.12

import time

import os


try:

    from watchdog.observers import Observer

    from watchdog.events import FileSystemEventHandler

except ModuleNotFoundError as e:

    print (e)

    os.system("pip install watchdog")


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

class Handler(FileSystemEventHandler):


    def on_created(self, event): # 파일 생성시


        print (f'event type : {event.event_type}\n'

               f'event src_path : {event.src_path}')

        if event.is_directory:

            print ("디렉토리 생성")

        else: # not event.is_directory

            """

            Fname : 파일 이름

            Extension : 파일 확장자 

            """

            Fname, Extension = os.path.splitext(os.path.basename(event.src_path))

            '''

             1. zip 파일

             2. exe 파일

             3. lnk 파일

            '''

            if Extension == '.zip':

                print (".zip 압축 파일 입니다.")

            elif Extension == '.exe':

                print (".exe 실행 파일 입니다.")

                os.remove(Fname + Extension)   # _파일 삭제 event 발생

            elif Extension == '.lnk':

                print (".lnk 링크 파일 입니다.")


    def on_deleted(self, event):

        print ("삭제 이벤트 발생")


    def on_moved(self, event): # 파일 이동시

        print (f'event type : {event.event_type}\n')


class Watcher:

    # 생성자

    def __init__(self, path):

        print ("감시 중 ...")

        self.event_handler = None      # Handler

        self.observer = Observer()     # Observer 객체 생성

        self.target_directory = path   # 감시대상 경로

        self.currentDirectorySetting() # instance method 호출 func(1)


    # func (1) 현재 작업 디렉토리

    def currentDirectorySetting(self):

        print ("====================================")

        print ("현재 작업 디렉토리:  ", end=" ")

        os.chdir(self.target_directory)

        print ("{cwd}".format(cwd = os.getcwd()))

        print ("====================================")


    # func (2)

    def run(self):

        self.event_handler = Handler() # 이벤트 핸들러 객체 생성

        self.observer.schedule(

            self.event_handler,

            self.target_directory,

            recursive=False

        )

        self.observer.start() # 감시 시작

        try:

            while True: # 무한 루프

                time.sleep(1) # 1초 마다 대상 디렉토리 감시

        except KeyboardInterrupt as e: # 사용자에 의해 "ctrl + z" 발생시

            print ("감시 중지...")

            self.observer.stop() # 감시 중단


myWatcher = Watcher("C:\\Users\\sleep\\Desktop\\tday")

myWatcher.run()

'언어 > python' 카테고리의 다른 글

python - 크롤링 - matplotlib - pie 차트 조합  (0) 2018.10.21
네이버 영화 크롤링 파이썬  (0) 2018.10.20
정규식  (0) 2018.10.16
virusshare malware hash crawling python  (0) 2018.10.12
python winlog event  (0) 2018.10.10