파이썬 코딩

2024.12.05. [파이썬] DART 기업개황 크롤링

37song 2025. 1. 15. 19:40

DART 기업개황에 있는 모든 기업의 정보를 크롤링 하기
동적페이지라, 어쩔수 없이... 오랜 시간에 걸쳐서 크롤링을 진행했다 !!!! 으!!


수십만 개의 회사 정보를 정리해보란다.. 에?😲
2024년 12월 기준으로
ㄱ으로 시작되는 회사 : 6,346개
ㄴ으로 시작되는 회사 : 3,630개
ㄷ으로 시작되는 회사 : 11,289개
가장 많은 ㅇ 은......27,416개

불과 3주전만 해도 미친 짓이라고 생각할 법한 일들을
자동으로 된다... 너무나 신기하고 놀랍다! 무엇보다 재밌다😁

모든걸 자동화 하기 위해 고민 했던 부분
1. 페이지를 넘기기 위한 선택자 찾기 (형제 선택자)
2. 각 자음마다 다른 쪽 수 해결
3. 마지막 쪽까지 정리 후 다음 자음 넘어가게 하기
요정도? 😎😎😎

 

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from bs4 import BeautifulSoup
import requests
import pandas as pd
import pyperclip
import time

url = 'https://dart.fss.or.kr/dsae001/main.do#none'

driver = webdriver.Chrome()
driver.get(url)
driver.maximize_window()
time.sleep(2)

for i in range (1, 17) :       # 파일이 ㄱ(1) ㄴ(2) ㄷ(3) ...mz(16)
    driver.find_element(By.CSS_SELECTOR, f'#ulIdx > li:nth-of-type({i}) > a').click()
    time.sleep(1)

    # 페이지 총 개수
    total = int(driver.find_element(By.CSS_SELECTOR, '#listContents > div.psWrap > div.pageInfo').text.split(']')[0].split('/')[1])
    # result 초기화
    result = []

    # 자음별 페이지 이동
    for x in range (total):
        # 각 회사 정보 수집 및 append
        for y in range (1, 46) :
            try:
                # 회사명 클릭
                driver.find_element(By.CSS_SELECTOR, f'#corpTabel > tbody > tr:nth-of-type({y}) > td.tL.ellipsis > span > a').click()
                time.sleep(1)

                try :
                    com_kr = driver.find_element(By.CSS_SELECTOR, '#corpDetailTabel > tbody > tr:nth-of-type(1) > td').text
                except :
                    com_kr = ''
                try :
                    com_en = driver.find_element(By.CSS_SELECTOR, '#corpDetailTabel > tbody > tr:nth-of-type(2) > td').text
                except :
                    com_en = ''
                try :
                    com_gong = driver.find_element(By.CSS_SELECTOR, '#corpDetailTabel > tbody > tr:nth-of-type(3) > td').text
                except :
                    com_gong = ''
                try :
                    item_code = driver.find_element(By.CSS_SELECTOR, '#corpDetailTabel > tbody > tr:nth-of-type(4) > td').text
                except :
                    item_code = ''
                try :
                    ceo = driver.find_element(By.CSS_SELECTOR, '#corpDetailTabel > tbody > tr:nth-of-type(5) > td').text
                except :
                    ceo = ''
                try :
                    corp_class = driver.find_element(By.CSS_SELECTOR, '#corpDetailTabel > tbody > tr:nth-of-type(6) > td').text
                except :
                    corp_class = ''
                try :
                    corp_no1 = driver.find_element(By.CSS_SELECTOR, '#corpDetailTabel > tbody > tr:nth-of-type(7) > td').text
                except :
                    corp_no1 = ''
                try :
                    corp_no2 = driver.find_element(By.CSS_SELECTOR, '#corpDetailTabel > tbody > tr:nth-of-type(8) > td').text
                except :
                    corp_no2 = ''
                try :
                    addr = driver.find_element(By.CSS_SELECTOR, '#corpDetailTabel > tbody > tr:nth-of-type(9) > td').text
                except :
                    addr = ''
                try :
                    homepage = driver.find_element(By.CSS_SELECTOR, '#homePage').text
                except :
                    homepage = ''
                try :
                    tel = driver.find_element(By.CSS_SELECTOR, '#corpDetailTabel > tbody > tr:nth-of-type(12) > td').text
                except :
                    tel = ''
                try :
                    fax = driver.find_element(By.CSS_SELECTOR, '#corpDetailTabel > tbody > tr:nth-of-type(13) > td').text
                except :
                    fax = ''
                try :
                    industry_name = driver.find_element(By.CSS_SELECTOR, '#corpDetailTabel > tbody > tr:nth-of-type(14) > td').text
                except :
                    industry_name = ''
                try :
                    est = driver.find_element(By.CSS_SELECTOR, '#corpDetailTabel > tbody > tr:nth-of-type(15) > td').text
                except :
                    est = ''

                finally:
                    result.append([com_kr, com_en, com_gong, item_code, ceo, corp_class, corp_no1, corp_no2, addr, homepage, tel, fax, industry_name, est])
            except :
                pass

        # 활성화된 페이지의 오른쪽 페이지 클릭
        driver.find_element(By.CSS_SELECTOR, '#listContents > div.psWrap > div.pageSkip > ul > li.on + li').click()
        time.sleep(2)

    df = pd.DataFrame(result, columns=['회사이름', '영문명','공시회사명', '종목코드','대표자명','법인구분','법인등록번호','사업자등록번호','주소','홈페이지','전화','팩스','업종명','설립일'])
    df.to_excel(f'dart ({i}).xlsx')