# 財報爬蟲超簡單：用 Python 一次抓綜合損益、資產負債、營益分析表

台股研究 2020年7月22日 約 1 分鐘閱讀

用 Python 與 pandas 對接公開資訊觀測站，一次抓取綜合損益、資產負債、營益分析三大季報彙總表，並用 pd.read\_html 轉成乾淨的 DataFrame。財報爬蟲是季報選股、財報選股與多因子策略最重要的資料起點，文章附完整函式可直接複製使用。

本系列已經介紹了各式各樣的爬蟲，從每天、每月，現在終於要季了，在尋找了各式各樣的網站，交叉比對之下，發現[公開資訊觀測站的總報表](http://mops.twse.com.tw/mops/web/t163sb04)還滿好抓的，所以就從這個網站下手。

Python中有個非常好用的package，叫做pandas，利用它我們就可以簡潔的把網頁上的表格，轉換成dataframe，也就是整理成程式看的懂的表格，我們幾乎不需要做太多的處理！這也是我喜歡用python一點。

這邊我用的是python3 喔～可能要注意一下，另外pandas的版本不一樣，可能會導致網頁轉dataframe的時候，有點小不一樣，可能要稍微檢查一下，我是用0.20.3這個版本。

財報爬蟲

顯示程式碼

```
import requests
import pandas as pd
import numpy as np
 
def financial_statement(year, season, type='綜合損益彙總表'):
 
    if year >= 1000:
        year -= 1911
 
    if type == '綜合損益彙總表':
        url = 'https://mops.twse.com.tw/mops/web/ajax_t163sb04'
    elif type == '資產負債彙總表':
        url = 'https://mops.twse.com.tw/mops/web/ajax_t163sb05'
    elif type == '營益分析彙總表':
        url = 'https://mops.twse.com.tw/mops/web/ajax_t163sb06'
    else:
        print('type does not match')
 
    r = requests.post(url, {
        'encodeURIComponent':1,
        'step':1,
        'firstin':1,
        'off':1,
        'TYPEK':'sii',
        'year':str(year),
        'season':str(season),
    })
 
    r.encoding = 'utf8'
    dfs = pd.read_html(r.text, header=None)
 
    return pd.concat(dfs[1:], axis=0, sort=False)\
             .set_index(['公司代號'])\
             .apply(lambda s: pd.to_numeric(s, errors='ceorce'))
```

依本文重跑

## 執行這個策略

複製提示詞，讓 AI 依照上方證據的資料、條件與風險檢查重跑一次。

告訴你的AI：

幫我設定 FinLab，重現這篇文章，請讀：https\://finlab.finance/setup?relatedUrl=/blog/python-financial-statement-crawler

[免安裝，開啟 FinLab Studio](https://studio.finlab.finance/chat?lang=zh\&research_context=%7B%22v%22%3A1%2C%22source_url%22%3A%22https%3A%2F%2Ffinlab.finance%2Fblog%2Fpython-financial-statement-crawler%22%7D)

這個function的用法，就跟月爬蟲是一樣的，可以直接輸入西元或國曆，然後第n季，只能輸入1～4季喔～不然會當掉，最後還要輸入哪一種財報：

* 綜合損益彙總表
* 資產負債彙總表
* 營益分析彙總表

都可以爬取喔！以下就是利用這個function所爬取的資料

[![Python 財報爬蟲爬取公開資訊觀測站綜合損益彙總表並轉成 pandas dataframe 的結果](https://r2.finlab.finance/blog/python-%E8%B2%A1%E5%A0%B1%E7%88%AC%E8%9F%B2-1-%E7%B6%9C%E5%90%88%E6%90%8D%E7%9B%8A%E8%A1%A8/result-1-1024x354.png)](https://r2.finlab.finance/blog/python-%E8%B2%A1%E5%A0%B1%E7%88%AC%E8%9F%B2-1-%E7%B6%9C%E5%90%88%E6%90%8D%E7%9B%8A%E8%A1%A8/result-1-1024x354.png)

大家可以看到有些columns怪怪的，例如 “合計：共 808 家” 這個是什麼鬼XDD，由於我一次爬了三個表，這一點小問題應該還好啦！XDD，可以再寫一行code刪除該列就好了。

基本上資料都是對齊的，不用太擔心。

爬完之後就交給大家自由發揮囉～～可以做很多事情。 總算各種股價、財報、月報的爬蟲都分享完了～

接下來就是真的利用這些資料來選股囉！

***

## 延伸閱讀

* [Python 財報月報股價爬蟲：台股資料庫解決之道](https://finlab.finance/blog/python-taiwan-stock-database)
* [台股財報發布日、截止日與時序索引完整教學](https://finlab.finance/blog/taiwan-financial-statement-date-index)
* [程式交易入門指南](https://finlab.finance/tools/program-trading)

**投資警語：本文僅供教學參考，不構成投資建議。過去績效不代表未來表現，投資有風險。**

![韓承佑](https://finlab.finance/people/cheng-yu-han-avatar.jpg)

韓承佑

創辦人 & 量化研究員

韓承佑是 FinLab 創辦人，也是開源 Python 量化套件 finlab 的作者，長年投入台股與美股的量化策略研究、回測與教學。

[在 GitHub 上追蹤 →](https://github.com/koreal6803) [在 LinkedIn 上連結 →](https://www.linkedin.com/in/cheng-yu-han/)

FinLab AI

### 想建立自己的策略？

用自然語言描述你的選股想法，AI 自動驗證、回測、給你答案

[免費開始](https://finlab.finance/#get-started)

## 更多多因子選股研究

[查看全部](https://finlab.finance/blog/category/factor)

* [![Claude 選股 Prompt：台股回測策略實戰](https://r2.finlab.finance/blog/ai-prompt-backtest-strategy/chart_thumbnail_ai_prompt_backtest_strategy_v5.png) Claude 選股 Prompt：台股回測策略實戰 年化報酬 48.9% · 台股](https://finlab.finance/blog/ai-prompt-backtest-strategy)
* [![現金減資選股：月營收動能加剛減資的股票，年化 48.7%、夏普 1.50](https://r2.finlab.finance/blog/cash-capital-reduction-revenue-momentum/chart_equity.png) 現金減資選股：月營收動能加剛減資的股票，年化 48.7%、夏普 1.50 年化報酬 48.7% · 台股](https://finlab.finance/blog/cash-capital-reduction-revenue-momentum)
* [![可轉債選股：月營收動能避開可轉債發行公司，16 年年化 42.2%、夏普 1.43](https://r2.finlab.finance/blog/convertible-bond-stock-selection/chart_equity.png) 可轉債選股：月營收動能避開可轉債發行公司，16 年年化 42.2%、夏普 1.43 年化報酬 42.2% · 台股](https://finlab.finance/blog/convertible-bond-stock-selection)
* [![營收驚喜選股：月營收 SURGE 前 30 檔年化 32.4%，夏普贏 0050](https://r2.finlab.finance/blog/monthly-revenue-surprise-strategy/chart_equity.png) 營收驚喜選股：月營收 SURGE 前 30 檔年化 32.4%，夏普贏 0050 年化報酬 32.4% · 台股](https://finlab.finance/blog/monthly-revenue-surprise-strategy)

來源：https://finlab.finance/blog/python-financial-statement-crawler｜資料日期：依上文各表格與段落標示｜頁面更新：2026-11-25
