# 用 Python 投資加密貨幣：爬蟲下載比特幣歷史數據 (Part 2)

美股研究 2020年7月20日 約 2 分鐘閱讀

用 Python 爬蟲從 Binance 下載 BTCUSDT 4 小時 K 線歷史價格，透過 finlab crypto 套件的 get\_all\_binance 一行取得所有資料並自動存成 CSV，適合 Colab 線上跑、後續接 backtesting 或 multicharts 回測加密貨幣策略。

手把手教你用 Python 爬蟲，從 Binance 下載比特幣 (BTCUSDT) 歷史價格數據並存成 CSV。

這個單元，我們分享如何用爬蟲獲取比特幣歷史價格！

可以打開 [colab](https://colab.research.google.com/) 線上編輯 python，跟著我們一起做實驗吧！ colab 是 google 提供，線上運行 python 的平台，打開後，新增一個 notebook 直接一步步貼上下面的代碼，程式就會順利運行了！

[![用 Python 爬蟲下載加密貨幣歷史數據教學縮圖](https://r2.finlab.finance/blog/btc-crawler-py/thumbnail-5-6.png)](https://r2.finlab.finance/blog/btc-crawler-py/thumbnail-5-6.png)

依本文重跑

## 執行這個策略

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

告訴你的AI：

幫我設定 FinLab，重現這篇文章，請讀：https\://finlab.finance/setup?relatedUrl=/blog/python-crypto-bitcoin-historical-data-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-crypto-bitcoin-historical-data-crawler%22%7D)

## 獲取 python 函式庫

首先，我們要先獲取幫大家寫好的 package，可以到 github 上下載：

顯示程式碼

```
!git clone https://github.com/koreal6803/crypto_backtrader.git
%cd crypto_backtrader
```

上面的指令，會先將預先寫好的程式從 github 上下載下來，並且將目錄切換到 crypto\_backtrader，也就是下載下來的資料夾中。

## 安裝一些必要的 packages

接下來我們來安裝一些必要的 dependencies，這樣等會才能順利執行程式：

顯示程式碼

```
!pip install python-binance
!pip install bitmex
!pip install Backtesting
!pip install backtrader
```

## 加密貨幣命名方法

這篇文章中，我們爬取 `BTCUSDT` 的價格，`BTCUSDT` 這個 symbol 可以拆逞兩個部分：一個是 `BTC`，另一個是 `USDT`。`BTCUSDT` 代表：`BTC` 的價格，以 `USDT` 為單位，這邊為什麼我們要用 `USDT` 呢？因為他是跟 `USD` 是美元是表兄弟，`USDT` 是一種跟 `USD` 約略 1:1 兌換的穩定虛擬貨幣，`USDT` 的好處是：

在交易時，假如用 加密貨幣 和 法定貨幣（美元）來回交易，這種方式交易，手續費會非常高昂，例如我們以 Maicoin 買入跟賣出手續費約高達 1%。

今天我們假如使用 `BTCUSDT` pair 來交易，由於同樣是加密貨幣，所以手續費算是大幅的降低，目前手續費在各平台，一般的標準是 0.1%，根據平台的優惠、交易量可以打折，成本算是低很多！

[交易對（trading pair）的相關介紹](http://blog.udn.com/mashmoro980727/121661372)

那廢話不多說，趕快來下載歷史資料吧！

## 使用幫大家寫好的加密貨幣爬蟲函式庫

我們可以使用： `from finlab import crypto` 將一些好用的 function 給叫進來！

然後用： `crypto.get_all_binance` 這個 function， 來獲取交易對（trading pair）。

顯示程式碼

```
from finlab import crypto
df = crypto.get_all_binance('BTCUSDT', '4h')
df.head()
```

[![使用 finlab crypto.get\_all\_binance 下載 BTCUSDT 歷史資料的 DataFrame 結果](https://r2.finlab.finance/blog/btc-crawler-py/df-3-1024x288.png)](https://r2.finlab.finance/blog/btc-crawler-py/df-3-1024x288.png)

上述這個 `get_all_binance`，除了幫你整理好下載資料外，還會將這些資料資料，存在 `crypto_backtrader/history/crypto/` 當中，以 `csv` 的方式存起來，方便你用 `excel`、`multicharts` 等程式來打開。

你也可以輸入不同的 trading pair 名稱，可以 [binance 官網](https://www.binance.com/tw/markets) 查找！

下一步，我們會分享： 怎麼用 [Python 回測一個簡單的交易策略](https://finlab.finance/blog/python-bitcoin-backtesting-sma-strategy)！

如果喜歡我們的文章，也歡迎參考我們最新募資的線上課程「[用 Python 理財：打造加密貨幣實戰策略](https://hahow.in/cr/crypto-python)」，在線上課程中，我們會更有系統、更完整地教您如何從無到有，打造 10 種加密貨幣實戰策略，並且能夠自動化雲端交易！有興趣的話，可以[點此觀看課程介紹影片](https://hahow.in/cr/crypto-python)喔！

***

## 延伸閱讀

* [比特幣最強指標 Hash Ribbons 原理篇（Part 5）](https://finlab.finance/blog/bitcoin-hash-ribbons-indicator-python)
* [打造加密貨幣實戰策略：量化投資新藍海](https://finlab.finance/blog/python-crypto-trading-strategy-course)
* [程式交易是什麼？完整入門指南](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)

## 更多AI 與程式研究

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

* [![AI 選股提示詞 30 條與真實回測：熱門提示詞為何失效，有效寫法長什麼樣](https://r2.finlab.finance/blog/ai-stock-picking-prompts-backtest/chart_equity_all.png) AI 選股提示詞 30 條與真實回測：熱門提示詞為何失效，有效寫法長什麼樣 年化報酬 30.9% · 台股](https://finlab.finance/blog/ai-stock-picking-prompts-backtest)
* [![Claude Code 量化交易實戰：裝 finlab 跑台股籌碼回測，從一句話到三因子迭代](https://r2.finlab.finance/blog/claude-code-finlab-quant-trading/chart_equity_2026-10-10.png) Claude Code 量化交易實戰：裝 finlab 跑台股籌碼回測，從一句話到三因子迭代 年化報酬 18.9% · 台股](https://finlab.finance/blog/claude-code-finlab-quant-trading)
* [![AI Agent 量化交易工具比較：Claude Code、Codex、Pi、Hermes、龍蝦怎麼選](https://r2.finlab.finance/blog/ai-agent-quant-trading-tools/chart_equity_2026-10-11.png) AI Agent 量化交易工具比較：Claude Code、Codex、Pi、Hermes、龍蝦怎麼選 年化報酬 18.3% · 台股](https://finlab.finance/blog/ai-agent-quant-trading-tools)
* [![AI 量化實測：Qwen 3 自動寫策略，12 次迭代結果](https://r2.finlab.finance/blog/qwen3-quant-assistant/chart_validation_2026-10-30.png) AI 量化實測：Qwen 3 自動寫策略，12 次迭代結果 年化報酬 14.0% · 台股](https://finlab.finance/blog/qwen3-quant-assistant)

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