Categories: '교육'

[업체 정보] 대표자, 주소, 매출을 한눈에 살펴보기 with python

[업체 정보] 대표자, 주소, 매출을 한눈에 살펴보기 with python l 가끔 업체 조사를 할 때면 각 업체의 대표자, 주소, 설립일, 매출 등을 살펴봅니다. 네이버에서 보면 나이스 신용평가에서 제공하는 서비스가 링크가 걸려있는데 이 링크를 클릭하면 그 회사의 정보를 얻을 수 있습니다. 하지만 여러 업체를 조사하려면 꽤 시간이 많이 걸리죠. 그래서 이 노가다 작업을 쉽게 하기 위해서 코드를 하나 만들어 보려고합니다.

[업체 정보] 대표자, 주소, 매출을 한눈에 살펴보기 with python

먼저 네이버에서 해당 정보를 얻기 위해서 구조를 살펴보면 업체를 검색한 뒤, 주소가 바뀌는 패던을 보면 kiscode 값이 바뀌는 것을 볼 수 있습니다. 업체의 고유 번호 같은 것인데, 이 코드를 받은 후 나이스 평가 사이트에서 이 값을 다시 넣어서 업체를 조회하는 패턴으로 코드를 작성해주려고 합니다.

[업체 정보] 대표자, 주소, 매출을 한눈에 살펴보기 with python

[나이스 평가 사이트에서는 무단 크롤링을 엄격히 금지하고 있으니 공부 목적으로만 사용하시기 바랍니다. 고유 지적 재산에 해당 할 수 있으니 이 코드를 유료로도 판매하지 마시고, 또 공부 외의 목적으로는 절대 남용하지 마십시오]

아래 보면 (주)삼성전자 의 KISCODE 를 확인할 수 있습니다.

[업체 정보] 대표자, 주소, 매출을 한눈에 살펴보기 with python
[업체 정보] 대표자, 주소, 매출을 한눈에 살펴보기 with python

이렇게 조회한 정보에서 대표자 ~ 상장일자 정보까지 추출해 보겠습니다.

import streamlit as st
import pandas as pd
import requests
from bs4 import BeautifulSoup
import ssl
from requests.adapters import HTTPAdapter
from urllib3.poolmanager import PoolManager
import re
import time

# SSL/TLS 설정을 위한 어댑터 클래스
class TLSAdapter(HTTPAdapter):
    def init_poolmanager(self, *args, **kwargs):
        context = ssl.create_default_context()
        context.set_ciphers('DEFAULT:@SECLEVEL=1')
        kwargs['ssl_context'] = context
        return super().init_poolmanager(*args, **kwargs)

# 세션 생성 및 어댑터 설정
session = requests.Session()
session.mount('https://', TLSAdapter())

# 네이버 검색 결과에서 kiscode 추출 함수
def find_kiscode_from_naver_search(query):
    search_url = 'https://search.naver.com/search.naver'
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'
    }
    params = {
        'where': 'nexearch',
        'sm': 'top_hty',
        'fbm': '0',
        'ie': 'utf8',
        'query': query
    }

    try:
        response = session.get(search_url, headers=headers, params=params)
        response.raise_for_status()
        soup = BeautifulSoup(response.content, 'html.parser')
        
        more_info_link = soup.find('a', class_='info_more', href=True)
        if more_info_link and 'kiscode=' in more_info_link['href']:
            kiscode = re.search(r'kiscode=([A-Za-z0-9]+)', more_info_link['href'])
            if kiscode:
                return kiscode.group(1), more_info_link['href']
        return None, None
    except requests.RequestException as e:
        st.error(f"Request failed: {e}")
        return None, None

# 나이스 사이트에서 업체 정보를 가져오는 함수
def get_company_info(kiscode):
    url = f'https://www.nicebizinfo.com/ep/EP0100M002GE.nice?kiscode={kiscode}'
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'
    }
    
    try:
        response = session.get(url, headers=headers)
        response.raise_for_status()
        soup = BeautifulSoup(response.content, 'html.parser')
        
        company_info = {
            '대표자': soup.find('p', string='대표자').find_next_sibling('strong').get_text(strip=True) if soup.find('p', string='대표자') else '',
            '본사주소': soup.find('p', string='본사주소').find_next_sibling('strong').get_text(strip=True) if soup.find('p', string='본사주소') else '',
            '그룹명': soup.find('p', string='그룹명').find_next_sibling('strong').get_text(strip=True) if soup.find('p', string='그룹명') else '',
            '사업자번호': soup.find('p', string='사업자번호').find_next_sibling('strong').get_text(strip=True) if soup.find('p', string='사업자번호') else '',
            '기업형태': soup.find('p', string='기업형태').find_next_sibling('strong').get_text(strip=True) if soup.find('p', string='기업형태') else '',
            '산업': soup.find('p', string='산업').find_next_sibling('strong').get_text(strip=True) if soup.find('p', string='산업') else '',
            '설립일자': soup.find('p', string='설립일자').find_next_sibling('strong').get_text(strip=True) if soup.find('p', string='설립일자') else '',
            '상장일자': soup.find('p', string='상장일자').find_next_sibling('strong').get_text(strip=True) if soup.find('p', string='상장일자') else ''
        }
        
        revenue_row = soup.find('tr', class_='bdBck fwb')
        if revenue_row:
            revenues = revenue_row.find_all('td', class_='tar')
            revenue_data = []
            for td in revenues:
                text = td.get_text(strip=True).replace(',', '').replace(' ', '')
                if text.isdigit():
                    revenue_data.append(f"{int(text):,} 원")
                else:
                    revenue_data.append(text)
        else:
            revenue_data = ['', '', '']
        
        return company_info, revenue_data
    except requests.RequestException as e:
        st.error(f"Request failed: {e}")
        return {}, ['', '', '']

# 스트림릿 웹 앱 구성
def main():
    st.title("업체 정보 조회기")
    
    # 옵션 선택: 직접 입력 vs 엑셀 업로드
    option = st.radio("업체명을 직접 입력하시겠습니까, 아니면 엑셀 파일을 업로드하시겠습니까?", ("직접 입력", "엑셀 업로드"))

    if option == "직접 입력":
        company_name = st.text_input("업체명을 입력하세요:")
        if st.button("조회"):
            kiscode, nice_info_url = find_kiscode_from_naver_search(company_name)
            if kiscode:
                company_info, revenue_data = get_company_info(kiscode)
                result = {
                    '업체명': company_name,
                    'kiscode': kiscode,
                    '대표자': company_info.get('대표자', ''),
                    '본사주소': company_info.get('본사주소', ''),
                    '그룹명': company_info.get('그룹명', ''),
                    '사업자번호': company_info.get('사업자번호', ''),
                    '기업형태': company_info.get('기업형태', ''),
                    '산업': company_info.get('산업', ''),
                    '설립일자': company_info.get('설립일자', ''),
                    '상장일자': company_info.get('상장일자', ''),
                    '2023년 매출': revenue_data[0],
                    '2022년 매출': revenue_data[1],
                    '2021년 매출': revenue_data[2]
                }
                st.write(result)
            else:
                st.write(f"업체 '{company_name}'를 찾을 수 없습니다.")
    
    elif option == "엑셀 업로드":
        uploaded_file = st.file_uploader("업로드할 엑셀 파일을 선택하세요.", type=["xlsx"])
        
        if uploaded_file is not None:
            df = pd.read_excel(uploaded_file)
            if '업체명' not in df.columns:
                st.error("엑셀 파일에 '업체명' 열이 존재하지 않습니다.")
                return
            
            results = []
            total_companies = len(df)
            completed_count = 0
            failed_count = 0
            progress_placeholder = st.empty()
            
            for idx, company_name in enumerate(df['업체명']):
                kiscode, nice_info_url = find_kiscode_from_naver_search(company_name)
                company_info, revenue_data = ({'대표자': '', '본사주소': '', '그룹명': '', '사업자번호': '', '기업형태': '', '산업': '', '설립일자': '', '상장일자': ''}, ['', '', '']) if not kiscode else get_company_info(kiscode)
                
                result = {
                    '업체명': company_name,
                    'kiscode': kiscode if kiscode else '',
                    '대표자': company_info.get('대표자', ''),
                    '본사주소': company_info.get('본사주소', ''),
                    '그룹명': company_info.get('그룹명', ''),
                    '사업자번호': company_info.get('사업자번호', ''),
                    '기업형태': company_info.get('기업형태', ''),
                    '산업': company_info.get('산업', ''),
                    '설립일자': company_info.get('설립일자', ''),
                    '상장일자': company_info.get('상장일자', ''),
                    '2023년 매출': revenue_data[0],
                    '2022년 매출': revenue_data[1],
                    '2021년 매출': revenue_data[2]
                }
                results.append(result)
                
                if kiscode:
                    completed_count += 1
                else:
                    failed_count += 1
                
                remaining_count = total_companies - (idx + 1)
                progress = (idx + 1) / total_companies * 100
                progress_placeholder.write(f"진척율: {progress:.2f}% | 완료: {completed_count}개 | 실패: {failed_count}개 | 남은 작업 수: {remaining_count}개")
                
                # 각 요청 사이에 3초 딜레이 추가
                time.sleep(1.5)
            
            # 결과 출력
            results_df = pd.DataFrame(results)
            if not results_df.empty:
                st.write(results_df)
            
            if failed_count > 0:
                st.write("다음 업체들은 정보를 찾을 수 없었습니다:")
                st.write([res['업체명'] for res in results if not res['kiscode']])
    
if __name__ == "__main__":
    main()

이렇게 추출한 코드는 streamlit에서 돌리면 아름다운 코드로 바뀌는 것을 볼 수 있습니다.

[업체 정보] 대표자, 주소, 매출을 한눈에 살펴보기 with python

urjent

Share
Published by
urjent

Recent Posts

베식타시 하포엘 베르셰바 유로파리그 중계 경기 일정

베식타시와 하포엘 베르셰바의 유로파리그 리그 페이즈 5차전 경기가 2026년 11월 27일 금요일 오전 2시 45분에…

1시간 ago

아마존 블랙프라이데이 할인 기간과 장바구니 필수 품목

아마존은 미국 현지 기준으로 11월 마지막 주에 열리는 블랙프라이데이 행사를 앞두고 이미 여러 할인 행사를…

2시간 ago

토익 한달 800점 달성 방법, 취업 기준 점수 넘기기

토익 한달 800점 고지를 넘으려면 기출문제 분석을 바탕으로 정답과 오답의 근거를 철저히 파악하는 공부법이 필수적입니다.…

3시간 ago

정은경 복지부 장관, 68세 연금지급 루머 직접 반박

"내년 연금이 밀린다"는 소문 때문에 밤잠을 설쳤던 분들, 오늘 정은경 복지부 장관의 공식 해명으로 마음이…

3시간 ago

송파구 아파트 가격 동향과 12월 행사 일정 확인하기

송파구에서는 연말을 앞두고 다채로운 지역 행사가 열리는 동시에 부동산 시장에는 잔잔한 변화의 바람이 불고 있어요.…

5시간 ago

박장범 KBS 사장 퇴진 논란, 파우치 사과 요구 이혼

박장범 KBS 사장이 정권 교체기에 다시 한 번 임기를 채우지 못하고 자리에서 물러나기로 했습니다. 개정…

7시간 ago