python selenium

언어/python2018. 11. 15. 17:22

https://stackoverflow.com/questions/10629815/handle-multiple-windows-in-python

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

from selenium.webdriver.chrome.options import Options

from selenium import webdriver

from bs4 import BeautifulSoup

from openpyxl import Workbook

from openpyxl.styles import Color, PatternFill

import pprint as ppr

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

class STU(object):

    # 생성자

    def __init__(self):

        self.RealTimeChart = dict() # type of dictionary

        self.hexColorList = list() # type of list

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

        self.options.headless = True

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

                                       chrome_options=self.options)

        self.html = None


        # Excel

        self.sheetIndex = [['B', 3], ['C', 3]]

        self.workBook = None

        self.workSheet = None


    # func (1)

    def get_url_requests(self):

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

        assert "NAVER" in self.driver.title

        print (self.driver.title)

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

        tmp_li_list = html.find_all("span", {"class": "ah_k"})

        for n, i in enumerate(tmp_li_list):

            self.RealTimeChart[str(n+1) + "순위"] = i.string

            print("{0:02d} :  {1:s}".format(n + 1, i.string))

        self.driver.execute_script("window.open()") # 새로운 창 open

        self.driver.switch_to.window(self.driver.window_handles[1])


    # func (2)

    def get_hex_color_list(self):

        self.driver.get("https://www.color-hex.com/")

        assert "Color Hex Color Codes" in self.driver.title

        print (self.driver.title)

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

        tmp_color_lst = html.select("div.colordvcon > a")

        for i in tmp_color_lst:

            hexColorValue = str(i.attrs['title']).split(sep=' ')

            hexColorValue = hexColorValue[0]

            self.hexColorList.append(hexColorValue)

        ppr.pprint (self.hexColorList)


    # func (3)

    def xlWrite(self):

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

        self.workSheet = self.workBook.create_sheet(title="실시간 순위")

        # 셀에 너비 =====================================================

        self.workSheet.column_dimensions['B'].width = 8.1

        self.workSheet.column_dimensions['C'].width = 17.2

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

        self.workSheet[self.sheetIndex[0][0] + str(self.sheetIndex[0][1])] = "순위";self.sheetIndex[0][1]+=1

        self.workSheet[self.sheetIndex[1][0] + str(self.sheetIndex[1][1])] = "내용";self.sheetIndex[1][1]+=1



        for n, w in enumerate(self.RealTimeChart.items()):

            self.workSheet[self.sheetIndex[0][0] + str(self.sheetIndex[0][1])] = w[0]

            self.workSheet[self.sheetIndex[0][0] + str(self.sheetIndex[0][1])].fill = \

                PatternFill(patternType='solid', fgColor=Color(self.hexColorList[n][1:]))

            self.sheetIndex[0][1]+=1

            self.workSheet[self.sheetIndex[1][0] + str(self.sheetIndex[1][1])] = w[1]

            self.workSheet[self.sheetIndex[0][0] + str(self.sheetIndex[1][1])].fill = \

                PatternFill(patternType='solid', fgColor=Color(self.hexColorList[n][1:]))

            self.sheetIndex[1][1]+=1




    # 소멸자

    def __del__(self):

        self.workBook.save(r'C:\Users\sleep\Desktop\cor_list\cartList.xlsx')


def main():

    sNode = STU() # STU에 해당하는 인스턴스 객체 생성

    sNode.get_url_requests()

    sNode.get_hex_color_list()

    sNode.xlWrite()

if __name__ == "__main__":

    main()

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

실패 -  (0) 2018.11.28
selenium + pandas + 연습 중  (0) 2018.11.15
hexcolor 사이트 python 크롤링  (0) 2018.11.06
사람인 크롤링 (version 2.3 - 2018-11-07)  (2) 2018.11.05
python mysql  (0) 2018.11.04