DART电子披露系统的库存量投资方法

DART电子披露系统的库存量投资方法

KissCuseMe
2025-03-11
3

我将解释如何在DART电子披露系统中抓取财务报表。 使用Python的请求,美丽的套件和Pandas图书馆。但是,实际使用时要小心更改网站结构,并且可以在服务器上加载过多的请求。


1。安装必要的库

pip install requests beautifulsoup4 pandas openpyxl

2。Dart披露搜索和财务报表爬网示例

import requests
from bs4 import BeautifulSoup
import pandas as pd
from urllib.parse import urljoin

# Search Criteria (e.g., Samsung Electronics (005930) Annual Report)
COMPANY_CODE = "005930"  # Stock Code
START_DATE = "20230101"  # Search Start Date (YYYYMMDD)
END_DATE = "20231231"    # Search End Date (YYYYMMDD)
REPORT_TYPE = "A001"     # A001: Annual Report, A002: Semi-Annual Report, A003: Quarterly Report

# DART Disclosure Search URL
SEARCH_URL = "http://dart.fss.or.kr/dsab001/search.ax"

def get_report_list():
    """Function to fetch the list of DART disclosure reports"""
    params = {
        "currentPage": 1,
        "maxResults": 10,
        "businessCode": COMPANY_CODE,
        "startDate": START_DATE,
        "endDate": END_DATE,
        "reportName": REPORT_TYPE
    }
    response = requests.get(SEARCH_URL, params=params)
    soup = BeautifulSoup(response.text, 'html.parser')
    return soup.select(".table_list tr")[1:]  # Extract rows excluding the header

def extract_excel_url(report_url):
    """Function to extract the Excel file URL from the report page"""
    response = requests.get(report_url)
    soup = BeautifulSoup(response.text, 'html.parser')
    excel_link = soup.select_one("a[href*='download.xbrl']")
    if excel_link:
        return urljoin(report_url, excel_link['href'])
    return None

def download_excel(url):
    """Function to download the Excel file and convert it into a DataFrame"""
    response = requests.get(url)
    with open("temp.xlsx", "wb") as f:
        f.write(response.content)
    return pd.read_excel("temp.xlsx", engine='openpyxl')

# Main Execution
if __name__ == "__main__":
    reports = get_report_list()
    for idx, report in enumerate(reports[:3]):  # Process up to 3 reports
        # Extract report title and link
        title = report.select_one("td:nth-child(3) a").text.strip()
        report_url = urljoin(SEARCH_URL, report.select_one("td:nth-child(3) a")['href'])
        
        print(f"[{idx+1}] Extracting data from {title}...")
        
        # Extract Excel file URL and download
        excel_url = extract_excel_url(report_url)
        if excel_url:
            df = download_excel(excel_url)
            print(df.head())  # Check the data
        else:
            print("Excel file not found.")

3。代码说明

  • 搜索条件设置:
    • Company_Code:S项目代码(例如三星电子= 005930)
    • report_type:A001(年度),A002(半年度),A003(分支)
    • 日期范围仅限于start_date和end_date。
  • 报告清单爬行:
    • 致电DART披露搜索API并获取报告列表。
    • 与美丽的小组解析HTML后,提取报告标题和链接。
  • Excel文件提取:
    • 在每个报告页面上,以XBRL格式查找并下载XBRL文件链接。
    • 将Excel文件读取到熊猫并将其转换为数据框架。
  • 警告
    • 动态内容处理:某些页面可以使用JavaScript动态加载。 您可能需要使用此-SELENIUM。
    • 数据 - 固化:每个公司的Excel文件结构可能不同,因此您需要添加列映射逻辑。
    • 法律限制:爬行时必须遵守DART的条款和条件。

基于此代码,您可以实现其他数据预处理和定量分析逻辑。

股票
量化
投资
爬行
飞镖

0

目录

  • 1。安装必要的库
  • 2。Dart披露搜索和财务报表爬网示例
  • 3。代码说明
此帖子是 Coupang 合作伙伴计划的一部分,可能包含推广链接,我可能会收到佣金。

服务条款隐私政策支持
© 2025
我希望我能提前知道
All rights reserved.