본문 바로가기
Python

python+selenium+headless chrome+openpyxl 예제

by 앗사비 2019. 2. 7.
728x90

레드마인 결함 개수 추출 후 엑셀 저장하기

from selenium import webdriver
from bs4 import BeautifulSoup
import re
import openpyxl

### 헤드리스 브라우저 셋팅 ###
options = webdriver.ChromeOptions()
options.add_argument('headless')
#options.add_argument('window-size=1920x1080')
#options.add_argument("disable-gpu")
driver = webdriver.Chrome('F:/~~~/~~~/chromedriver.exe', chrome_options=options)

### 실제 동작 ###

#로그인 
driver.get('https://~~~')
driver.implicitly_wait(5)
driver.find_element_by_name('username').send_keys('~~~') 
driver.find_element_by_name('password').send_keys('~~~')
driver.find_element_by_name('password').submit()


#이름과 주소
project = [
    'test a','test b','
]
url = [
    #test a
    'https://~~~',
    #test b
    'https://~~~',
]

#엑셀 열기
wb = openpyxl.Workbook()
sheet = wb.active

#필드명
sheet.cell(row=1, column=1).value = '프로젝트명'
sheet.cell(row=1, column=2).value = '개수'

#개수
row_index = 2
culumn_index = 2   
for a in url:
    driver.get(a)
    driver.implicitly_wait(5)
    html = driver.page_source
    soup = BeautifulSoup(html, 'html.parser')
    total = soup.select('~~~') #크롬 개발자도구에서 Copy Selector 로 가져옴
    sheet.cell(row=row_index, column=1).value = project[row_index-2]
    for n in total:
        split = re.split('[\(\/\)]', n.text) #태그 분리
        result = int(split[0]) #형 변환
        sheet.cell(row=row_index, column=culumn_index).value = result
    if culumn_index % 2 == 0 :
        row_index = row_index + 1 
        culumn_index = 1
    culumn_index = culumn_index + 1 

#엑셀 닫기
wb.save('redmine-bug.xlsx')

#종료
driver.quit()


728x90