반응형
In [17]:
from time import sleep
import pyperclip as cb
import pandas as pd
from selenium import webdriver
import win32com.client as win32
In [18]:
driver = webdriver.Chrome(r"./chromedriver.exe")
driver.get('http://dart.fss.or.kr/dsac001/mainAll.do')
In [20]:
# onclick_list = []
# for i in driver.find_elements_by_css_selector('a[href^="/dsaf001/main.do?rcpNo="]'):
# onclick_list.append(i.get_attribute('onclick'))
onclick_list = [
i.get_attribute('onclick') for i in driver.find_elements_by_css_selector('a[href^="/dsaf001/main.do?rcpNo="]')]
onclick_list
Out[20]:
In [21]:
link_list = []
for i in onclick_list[:5]:
driver.execute_script(i)
driver.switch_to.window(driver.window_handles[1])
driver.execute_script(driver.find_element_by_css_selector('a[href="#download"]').get_attribute('onclick'))
driver.switch_to.window(driver.window_handles[2])
current_len = len(link_list)
link_list.append(driver.find_element_by_css_selector('a[href^="/pdf"]').get_attribute('href'))
while True:
if len(link_list) != current_len:
break
else:
sleep(0.1)
print(link_list[-1])
driver.close()
driver.switch_to.window(driver.window_handles[1])
driver.close()
driver.switch_to.window(driver.window_handles[0])
In [22]:
# 너무 오래 걸리는 관계로...
import pickle
with open(r"C:\Users\smj02\Desktop\hwp-파이썬관련코드\link_list.pickle", 'rb') as f:
link_list = pickle.load(f)
In [24]:
link_list[:10]
Out[24]:
In [31]:
# driver.page_source
# table = pd.read_html(driver.page_source)
table = pd.read_html(driver.page_source)[0]
driver.close()
In [32]:
table.head()
Out[32]:
In [33]:
len(table)
Out[33]:
In [34]:
len(table.columns)
Out[34]:
In [35]:
hwp = win32.Dispatch('HWPFrame.HwpObject')
hwp.RegisterModule("FilePathCheckDLL", "SecurityModule") # 보안모듈
Out[35]:
In [36]:
hwp.Open(r"C:\Users\smj02\Desktop\dart_template.hwp")
Out[36]:
In [38]:
def hwp_insert_hyperlink(text, url):
hwp.HAction.Run("TableCellBlock")
hwp.HAction.GetDefault("InsertHyperlink", hwp.HParameterSet.HHyperLink.HSet)
hwp.HParameterSet.HHyperLink.Text = text
hwp.HParameterSet.HHyperLink.Command = url
hwp.HAction.Execute("InsertHyperlink", hwp.HParameterSet.HHyperLink.HSet)
In [37]:
def hwp_insert_text(text):
hwp.HAction.GetDefault("InsertText", hwp.HParameterSet.HInsertText.HSet)
hwp.HParameterSet.HInsertText.Text = text
hwp.HAction.Execute("InsertText", hwp.HParameterSet.HInsertText.HSet)
In [ ]:
# for i in len(table):
# for j in table.loc[i]:
# cb.copy(j)
# hwp.Run('Paste')
# hwp.Run('TableRightCellAppend')
# hwp.Run('TableAppendRow')
In [39]:
# 좀 더 다듬어보면,
for i in range(len(table)):
for idx, text in enumerate(table.loc[i]):
if idx == 0 and i == 0:
hwp_insert_text(text)
elif idx == 2: # 보고서명 칼럼, 하이퍼링크를 삽입해야 한다.
hwp.Run('TableRightCellAppend')
hwp_insert_hyperlink(text=text, url='{};1;0;0'.format(link_list[i].replace("?","\\?")))
elif idx == 5: # 오른쪽 끝 비고란, 전부 Nan이므로 아무것도 입력하지 않고 넘어간다.
hwp.Run('TableRightCellAppend')
else: # 그 외에는 텍스트를 그대로 입력한다.
hwp.Run('TableRightCellAppend')
hwp_insert_text(text)
In [ ]:
회사원코딩이 추천하는 오늘자 파이썬 입문교재 3종
1. 모두의 파이썬 X 알고리즘(합본호):누구나 쉽게 배우는 프로그래밍 기초
모두의 파이썬 X 알고리즘(합본호):누구나 쉽게 배우는 프로그래밍 기초
COUPANG
www.coupang.com
파이썬으로 프로그래밍에 처음 입문하기엔 아주 재미있고 쉽게 배울 수 있음.
다른언어 사용자가 파이썬을 배우는 교재로는 비추천. 다소 가벼운 느낌.
2. 초보자를 위한 파이썬 200제
초보자를 위한 파이썬 200제
COUPANG
www.coupang.com
깔끔한 예제와 소스에 대한 자세한 설명이 돋보이는 교재.
입문-초급-중급-고급 난이도로 단계별 도전이 가능한 꽤 괜찮은 입문서.
3. 혼자 공부하는 첫 프로그래밍 with 파이썬:1:1 과외하듯 배우는 왕초보 코딩 입문서
혼자 공부하는 첫 프로그래밍 with 파이썬:1:1 과외하듯 배우는 왕초보 코딩 입문서
COUPANG
www.coupang.com
초유의 베스트셀러 "혼공파"마저도 당신에게 어렵게 느껴진다면?
유튜브 무료강의, 온라인실습과 다양한 이미지를 통해
제목대로 "1:1과외하듯" 파이썬을 배워볼 수 있음.
반응형
댓글