最近勤加練習 Python 應用, 發現 Python 應用實在既深且廣, 不斷的有高手將爬蟲寫成模組分享出來, 在 PyPi 網站隨時都有驚奇出現. 今天在 "
行銷資料科學 (Medium)" 看到下面這拚文章, 介紹用 twder 模組抓取台銀即時外匯報價的方法, 省去自行撰寫網頁爬蟲的麻煩, 真是 Fintech 的好工具 :
#
一秒就上手!馬上幫您爬取匯率價格!掌控最新貿易匯損!(附python 程式碼)
此模組作者為
Jimms Hsieh, 並將其發布在 PyPi 網站 :
#
https://pypi.org/project/twder/
線上安裝過程 :
D:\Python\E_14_1>
pip3 install twder
Collecting twder
Downloading https://files.pythonhosted.org/packages/03/3b/49ab585b6cd2734afee4227f5a344a110419e3eba298269eb87a943a980b/twder-0.1.1-py2.py3-none-any.whl
Requirement already satisfied: lxml in c:\python36\lib\site-packages (from twder) (4.2.1)
Requirement already satisfied: requests in c:\python36\lib\site-packages (from twder) (2.18.4)
Requirement already satisfied: idna<2 .7="">=2.5 in c:\python36\lib\site-packages (from requests->twder) (2.6)2>
Requirement already satisfied: chardet<3 .1.0="">=3.0.2 in c:\python36\lib\site-packages (from requests->twder) (3.0.4)3>
Requirement already satisfied: urllib3<1 .23="">=1.21.1 in c:\python36\lib\site-packages (from requests->twder) (1.22)1>
Requirement already satisfied: certifi>=2017.4.17 in c:\python36\lib\site-packages (from requests->twder) (2018.4.16)
Installing collected packages: twder
Successfully installed twder-0.1.1
從安裝過程可知, 此模組使用 requests 與 lxml 等等來爬台銀外匯報價網頁. 如果因為防火牆無法用 pip 安裝, 可到 Pypi 網站下載 whl 安裝檔 twder-0.1.1-py2.py3-none-any.whl :
#
https://pypi.org/project/twder/#files
本地安裝過程 :
D:\Python>
pip3 install twder-0.1.1-py2.py3-none-any.whl
Processing d:\python\twder-0.1.1-py2.py3-none-any.whl
Requirement already satisfied: requests in c:\python36\lib\site-packages (from twder==0.1.1) (2.13.0
)
Collecting lxml (from twder==0.1.1)
安裝好 twder 模組即可利用其 API 抓取台銀匯率報價, 使用方法見作者之 GitHub :
#
https://github.com/jimms/twder
從 twder 目錄裡面的
api.py 可窺知其爬蟲做法 (高手不吝分享的絕技非常值得學習), 擷取對象是台銀外匯報價網頁 :
#
http://rate.bot.com.tw/xrt?Lang=zh-TW (即時)
擷取歷史匯價的 URL 格式如下 :
http://rate.bot.com.tw/xrt/quote/{range}/{currency}
其中 range 為 "年-月", 例如 2018-10; 而 currency 為貨幣代號, 例如 USD (美金), JPY (日元), CNY (人民幣), EUR (歐元) 等等. 例如擷取 2018 年 10 月之美元歷史匯率報價, 其 URL 為 :
http://rate.bot.com.tw/xrt/quote/2018-10/USD
使用 twder 前須先匯入模組 :
import twder
此模組提供如下函數 :
twder 模組的函數 | 說明 |
currencies() | 傳回全部外幣代號 (list) |
currency_name_dict() | 傳回全部外幣代號與其中文名稱 (dict) |
now(currency) | 傳回指定外幣目前之報價 (tuple) |
now_all() | 傳回全部外幣目前之報價 (dict) |
past_day(currency) | 傳回指定外幣昨日全部報價 (list) |
past_six_month(currency) | 傳回指定外幣過去半年每天之收盤價 (list) |
specify_month(currency, year, month) | 傳回指定外幣在指定年月每天之收盤價 (list) |
1 : 列出台銀可交易之外幣代號
currencies() 只傳回幣別代號串列, 而 currency_name_dict() 則附上外幣中文名稱字典.
>>>
import twder
>>>
twder.currencies()
['USD', 'HKD', 'GBP', 'AUD', 'CAD', 'SGD', 'CHF', 'JPY', 'ZAR', 'SEK', 'NZD', 'THB', 'PHP', 'IDR', 'EUR', 'KRW', 'VND', 'MYR', 'CNY']
>>>
twder.currency_name_dict()
{'USD': '美金 (USD)', 'HKD': '港幣 (HKD)', 'GBP': '英鎊 (GBP)', 'AUD': '澳幣 (AUD)', 'CAD': '加拿大幣 (CAD)', 'SGD': '新加坡幣 (SGD)', 'CHF': '瑞士法郎 (CHF)', 'JPY': '日圓 (JPY)', 'ZAR': '南非幣 (ZAR)', 'SEK': '瑞典幣 (SEK)', 'NZD': '紐元 (NZD)', 'THB': '泰幣 (THB)', 'PHP': '菲國比索 (PHP)', 'IDR': '印尼幣 (IDR)', 'EUR': '歐元 (EUR)', 'KRW': '韓元 (KRW)', 'VND': '越南盾 (VND)', 'MYR': '馬來幣 (MYR)', 'CNY': '人民幣 (CNY)'}
可見總共有 19 種可交易外幣
2. 查詢指定貨幣目前的即時報價
>>>
twder.now('USD')
('2018/10/23 16:01', '30.54', '31.23', '30.91', '31.01')
傳回之 tuple 有五個元素 :
(時間, 現金買入, 現金賣出, 即期買入, 即期賣出)
3. 查詢全部貨幣目前的即時報價
>>>
twder.now_all()
{'USD': ('2018/10/23 16:01', '30.54', '31.23', '30.91', '31.01'), 'HKD': ('2018/10/23 16:01', '3.783', '3.999', '3.919', '3.979'), 'GBP': ('2018/10/23 16:01', '39.01', '41.13', '40.01', '40.43'), 'AUD': ('2018/10/23 16:01', '21.55', '22.33', '21.82', '22.05'), 'CAD': ('2018/10/23 16:01', '23.16', '24.07', '23.55', '23.77'), 'SGD': ('2018/10/23 16:01', '21.88', '22.79', '22.37', '22.55'), 'CHF': ('2018/10/23 16:01', '30.29', '31.49', '30.95', '31.24'), 'JPY': ('2018/10/23 16:01', '0.2663', '0.2791', '0.2736', '0.2776'), 'ZAR': ('2018/10/23 16:01', '-', '-', '2.11', '2.19'), 'SEK': ('2018/10/23 16:01', '3.05', '3.57', '3.39', '3.49'), 'NZD': ('2018/10/23 16:01', '19.85', '20.7', '20.23', '20.43'), 'THB': ('2018/10/23 16:01', '0.819', '1.007', '0.9276', '0.9676'), 'PHP': ('2018/10/23 16:01', '0.5018', '0.6348', '-', '-'), 'IDR': ('2018/10/23 16:01', '0.00168', '0.00238', '-', '-'), 'EUR': ('2018/10/23 16:01', '34.69', '36.03', '35.31', '35.71'), 'KRW': ('2018/10/23 16:01', '0.02551', '0.02941', '-', '-'), 'VND': ('2018/10/23 16:01', '0.00095', '0.00145', '-', '-'), 'MYR': ('2018/10/23 16:01', '6.344', '7.974', '-', '-'), 'CNY': ('2018/10/23 16:01', '4.36', '4.522', '4.432', '4.482')}
4. 查詢指定貨幣昨日全部報價
>>>
twder.past_day('USD')
[('2018/10/23 09:01:01', '30.52', '31.21', '30.89', '30.99'), ('2018/10/23 09:02:55', '30.525', '31.215', '30.895', '30.995'), ('2018/10/23 09:04:30', '30.535', '31.225', '30.905', '31.005'), ('2018/10/23 09:05:43', '30.54', '31.23', '30.91', '31.01'), ('2018/10/23 09:07:02', '30.53', '31.22', '30.9', '31'), ('2018/10/23 09:12:28', '30.525', '31.215', '30.895', '30.995'), ('2018/10/23 09:15:49', '30.525', '31.215', '30.895', '30.995'), ('2018/10/23 09:17:17', '30.52', '31.21', '30.89', '30.99'), ('2018/10/23 09:21:57', '30.515', '31.205', '30.885', '30.985'), ('2018/10/23 09:24:32', '30.51', '31.2', '30.88', '30.98'), ('2018/10/23 09:28:09', '30.515', '31.205', '30.885', '30.985'), ('2018/10/23 09:35:59', '30.51', '31.2', '30.88', '30.98'), ('2018/10/23 09:38:46', '30.51', '31.2', '30.88', '30.98'), ('2018/10/23 09:40:44', '30.51', '31.2', '30.88', '30.98'), ('2018/10/23 09:48:51', '30.52', '31.21', '30.89', '30.99'), ('2018/10/23 09:50:10', '30.525', '31.215', '30.895', '30.995'), ('2018/10/23 09:52:31', '30.525', '31.215', '30.895', '30.995'), ('2018/10/23 09:58:44', '30.525', '31.215', '30.895', '30.995'), ('2018/10/23 10:05:43', '30.525', '31.215', '30.895', '30.995'), ('2018/10/23 10:15:33', '30.52', '31.21', '30.89', '30.99'), ('2018/10/23 10:21:54', '30.525', '31.215', '30.895', '30.995'), ('2018/10/23 10:26:31', '30.525', '31.215', '30.895', '30.995'), ('2018/10/23 10:28:29', '30.53', '31.22', '30.9', '31'), ('2018/10/23 10:30:59', '30.53', '31.22', '30.9', '31'), ('2018/10/23 10:45:50', '30.53', '31.22', '30.9', '31'), ('2018/10/23 10:59:00', '30.54', '31.23', '30.91', '31.01'), ('2018/10/23 10:59:46', '30.545', '31.235', '30.915', '31.015'), ('2018/10/23 11:01:47', '30.535', '31.225', '30.905', '31.005'), ('2018/10/23 11:20:48', '30.54', '31.23', '30.91', '31.01'), ('2018/10/23 11:36:05', '30.54', '31.23', '30.91', '31.01'), ('2018/10/23 11:44:24', '30.54', '31.23', '30.91', '31.01'), ('2018/10/23 11:54:21', '30.545', '31.235', '30.915', '31.015'), ('2018/10/23 11:57:05', '30.54', '31.23', '30.91', '31.01'), ('2018/10/23 12:21:57', '30.54', '31.23', '30.91', '31.01'), ('2018/10/23 12:30:55', '30.54', '31.23', '30.91', '31.01'), ('2018/10/23 12:47:50', '30.54', '31.23', '30.91', '31.01'), ('2018/10/23 13:06:57', '30.54', '31.23', '30.91', '31.01'), ('2018/10/23 13:21:23', '30.54', '31.23', '30.91', '31.01'), ('2018/10/23 13:41:52', '30.54', '31.23', '30.91', '31.01'), ('2018/10/23 13:52:33', '30.54', '31.23', '30.91', '31.01'), ('2018/10/23 13:59:38', '30.54', '31.23', '30.91', '31.01'), ('2018/10/23 14:02:10', '30.545', '31.235', '30.915', '31.015'), ('2018/10/23 14:03:06', '30.55', '31.24', '30.92', '31.02'), ('2018/10/23 14:11:39', '30.56', '31.25', '30.93', '31.03'), ('2018/10/23 14:19:02', '30.57', '31.26', '30.94', '31.04'), ('2018/10/23 14:23:07', '30.565', '31.255', '30.935', '31.035'), ('2018/10/23 14:24:54', '30.56', '31.25', '30.93', '31.03'), ('2018/10/23 14:26:10', '30.555', '31.245', '30.925', '31.025'), ('2018/10/23 14:33:29', '30.55', '31.24', '30.92', '31.02'), ('2018/10/23 14:34:03', '30.545', '31.235', '30.915', '31.015'), ('2018/10/23 14:42:06', '30.545', '31.235', '30.915', '31.015'), ('2018/10/23 14:44:05', '30.545', '31.235', '30.915', '31.015'), ('2018/10/23 14:46:08', '30.55', '31.24', '30.92', '31.02'), ('2018/10/23 14:54:23', '30.555', '31.245', '30.925', '31.025'), ('2018/10/23 14:55:39', '30.555', '31.245', '30.925', '31.025'), ('2018/10/23 14:56:56', '30.555', '31.245', '30.925', '31.025'), ('2018/10/23 14:58:30', '30.55', '31.24', '30.92', '31.02'), ('2018/10/23 14:59:08', '30.545', '31.235', '30.915', '31.015'), ('2018/10/23 14:59:53', '30.54', '31.23', '30.91', '31.01'), ('2018/10/23 14:59:56', '30.54', '31.23', '30.91', '31.01'), ('2018/10/23 15:02:22', '30.545', '31.235', '30.915', '31.015'), ('2018/10/23 15:04:53', '30.55', '31.24', '30.92', '31.02'), ('2018/10/23 15:20:15', '30.55', '31.24', '30.92', '31.02'), ('2018/10/23 15:21:26', '30.545', '31.235', '30.915', '31.015'), ('2018/10/23 15:33:32', '30.545', '31.235', '30.915', '31.015'), ('2018/10/23 15:37:56', '30.545', '31.235', '30.915', '31.015'), ('2018/10/23 15:38:09', '30.545', '31.235', '30.915', '31.015'), ('2018/10/23 15:42:42', '30.54', '31.23', '30.91', '31.01'), ('2018/10/23 16:01:16', '30.54', '31.23', '30.91', '31.01')]
5. 查詢指定貨幣過去半年每天收盤價
>>> t
wder.past_six_month('USD')
[('2018/10/23', '30.54', '31.23', '30.91', '31.01'), ('2018/10/22', '30.495', '31.185', '30.865', '30.965'), ('2018/10/19', '30.53', '31.22', '30.9', '31'), ('2018/10/18', '30.53', '31.22', '30.9', '31'), ('2018/10/17', '30.41', '31.1', '30.78', '30.88'), ('2018/10/16', '30.485', '31.175', '30.855', '30.955'), ('2018/10/15', '30.5', '31.19', '30.87', '30.97'), ('2018/10/12', '30.44', '31.13', '30.81', '30.91'), ('2018/10/11', '30.68', '31.37', '31.05', '31.15'), ('2018/10/09', '30.54', '31.23', '30.91', '31.01'), ('2018/10/08', '30.505', '31.195', '30.875', '30.975'), ('2018/10/05', '30.405', '31.095', '30.775', '30.875'), ('2018/10/04', '30.36', '31.05', '30.73', '30.83'), ('2018/10/03', '30.24', '30.93', '30.61', '30.71'), ('2018/10/02', '30.245', '30.935', '30.615', '30.715'), ('2018/10/01', '30.11', '30.8', '30.48', '30.58'), ('2018/09/28', '30.105', '30.795', '30.475', '30.575'), ('2018/09/27', '30.17', '30.86', '30.54', '30.64'), ('2018/09/26', '30.27', '30.96', '30.64', '30.74'), ('2018/09/25', '30.24', '30.93', '30.61', '30.71'), ('2018/09/21', '30.28', '30.97', '30.65', '30.75'), ('2018/09/20', '30.37', '31.06', '30.74', '30.84'), ('2018/09/19', '30.36', '31.05', '30.73', '30.83'), ('2018/09/18', '30.385', '31.075', '30.755', '30.855'), ('2018/09/17', '30.38', '31.07', '30.75', '30.85'), ('2018/09/14', '30.32', '31.01', '30.69', '30.79'), ('2018/09/13', '30.38', '31.07', '30.75', '30.85'), ('2018/09/12', '30.385', '31.075', '30.755', '30.855'), ('2018/09/11', '30.385', '31.075', '30.755', '30.855'), ('2018/09/10', '30.39', '31.08', '30.76', '30.86'), ('2018/09/07', '30.345', '31.035', '30.715', '30.815'), ('2018/09/06', '30.37', '31.06', '30.74', '30.84'), ('2018/09/05', '30.375', '31.065', '30.745', '30.845'), ('2018/09/04', '30.33', '31.02', '30.7', '30.8'), ('2018/09/03', '30.28', '30.97', '30.65', '30.75'), ('2018/08/31', '30.295', '30.985', '30.665', '30.765'), ('2018/08/30', '30.255', '30.945', '30.625', '30.725'), ('2018/08/29', '30.275', '30.965', '30.645', '30.745'), ('2018/08/28', '30.3', '30.99', '30.67', '30.77'), ('2018/08/27', '30.345', '31.035', '30.715', '30.815'), ('2018/08/24', '30.355', '31.045', '30.725', '30.825'), ('2018/08/23', '30.36', '31.05', '30.73', '30.83'), ('2018/08/22', '30.305', '30.995', '30.675', '30.775'), ('2018/08/21', '30.3', '30.99', '30.67', '30.77'), ('2018/08/20', '30.34', '31.03', '30.71', '30.81'), ('2018/08/17', '30.375', '31.065', '30.745', '30.845'), ('2018/08/16', '30.425', '31.115', '30.795', '30.895'), ('2018/08/15', '30.43', '31.12', '30.8', '30.9'), ('2018/08/14', '30.385', '31.075', '30.755', '30.855'), ('2018/08/13', '30.39', '31.08', '30.76', '30.86'), ('2018/08/10', '30.28', '30.97', '30.65', '30.75'), ('2018/08/09', '30.2', '30.89', '30.57', '30.67'), ('2018/08/08', '30.195', '30.885', '30.565', '30.665'), ('2018/08/07', '30.19', '30.88', '30.56', '30.66'), ('2018/08/06', '30.205', '30.895', '30.575', '30.675'), ('2018/08/03', '30.28', '30.97', '30.65', '30.75'), ('2018/08/02', '30.25', '30.94', '30.62', '30.72'), ('2018/08/01', '30.18', '30.87', '30.55', '30.65'), ('2018/07/31', '30.185', '30.875', '30.555', '30.655'), ('2018/07/30', '30.18', '30.87', '30.55', '30.65'), ('2018/07/27', '30.165', '30.855', '30.535', '30.635'), ('2018/07/26', '30.14', '30.83', '30.51', '30.61'), ('2018/07/25', '30.2', '30.89', '30.57', '30.67'), ('2018/07/24', '30.28', '30.97', '30.65', '30.75'), ('2018/07/23', '30.22', '30.91', '30.59', '30.69'), ('2018/07/20', '30.285', '30.975', '30.655', '30.755'), ('2018/07/19', '30.205', '30.895', '30.575', '30.675'), ('2018/07/18', '30.155', '30.845', '30.525', '30.625'), ('2018/07/17', '30.08', '30.77', '30.45', '30.55'), ('2018/07/16', '30.15', '30.84', '30.52', '30.62'), ('2018/07/13', '30.13', '30.82', '30.5', '30.6'), ('2018/07/12', '30.11', '30.8', '30.48', '30.58'), ('2018/07/11', '30.045', '30.735', '30.415', '30.515'), ('2018/07/10', '29.965', '30.655', '30.335', '30.435'), ('2018/07/09', '29.95', '30.64', '30.32', '30.42'), ('2018/07/06', '30.07', '30.76', '30.44', '30.54'), ('2018/07/05', '30.1', '30.79', '30.47', '30.57'), ('2018/07/04', '30.065', '30.755', '30.435', '30.535'), ('2018/07/03', '30.16', '30.85', '30.53', '30.63'), ('2018/07/02', '30.09', '30.78', '30.46', '30.56'), ('2018/06/29', '30.11', '30.652', '30.41', '30.51'), ('2018/06/28', '30.225', '30.767', '30.525', '30.625'), ('2018/06/27', '30.095', '30.637', '30.395', '30.495'), ('2018/06/26', '30.05', '30.592', '30.35', '30.45'), ('2018/06/25', '30.035', '30.577', '30.335', '30.435'), ('2018/06/22', '29.95', '30.492', '30.25', '30.35'), ('2018/06/21', '29.935', '30.477', '30.235', '30.335'), ('2018/06/20', '29.79', '30.332', '30.09', '30.19'), ('2018/06/19', '29.83', '30.372', '30.13', '30.23'), ('2018/06/15', '29.635', '30.177', '29.935', '30.035'), ('2018/06/14', '29.58', '30.122', '29.88', '29.98'), ('2018/06/13', '29.53', '30.072', '29.83', '29.93'), ('2018/06/12', '29.495', '30.037', '29.795', '29.895'), ('2018/06/11', '29.46', '30.002', '29.76', '29.86'), ('2018/06/08', '29.455', '29.997', '29.755', '29.855'), ('2018/06/07', '29.365', '29.907', '29.665', '29.765'), ('2018/06/06', '29.375', '29.917', '29.675', '29.775'), ('2018/06/05', '29.455', '29.997', '29.755', '29.855'), ('2018/06/04', '29.465', '30.007', '29.765', '29.865'), ('2018/06/01', '29.49', '30.032', '29.79', '29.89'), ('2018/05/31', '29.6', '30.142', '29.9', '30'), ('2018/05/30', '29.68', '30.222', '29.98', '30.08'), ('2018/05/29', '29.63', '30.172', '29.93', '30.03'), ('2018/05/28', '29.58', '30.122', '29.88', '29.98'), ('2018/05/25', '29.595', '30.137', '29.895', '29.995'), ('2018/05/24', '29.57', '30.112', '29.87', '29.97'), ('2018/05/23', '29.61', '30.152', '29.91', '30.01'), ('2018/05/22', '29.55', '30.092', '29.85', '29.95'), ('2018/05/21', '29.635', '30.177', '29.935', '30.035'), ('2018/05/18', '29.555', '30.097', '29.855', '29.955'), ('2018/05/17', '29.545', '30.087', '29.845', '29.945'), ('2018/05/16', '29.525', '30.067', '29.825', '29.925'), ('2018/05/15', '29.5', '30.042', '29.8', '29.9'), ('2018/05/14', '29.38', '29.922', '29.68', '29.78'), ('2018/05/11', '29.41', '29.952', '29.71', '29.81'), ('2018/05/10', '29.51', '30.052', '29.81', '29.91'), ('2018/05/09', '29.575', '30.117', '29.875', '29.975'), ('2018/05/08', '29.43', '29.972', '29.73', '29.83'), ('2018/05/07', '29.405', '29.947', '29.705', '29.805'), ('2018/05/04', '29.355', '29.897', '29.655', '29.755'), ('2018/05/03', '29.375', '29.917', '29.675', '29.775'), ('2018/05/02', '29.385', '29.927', '29.685', '29.785'), ('2018/04/30', '29.255', '29.797', '29.555', '29.655'), ('2018/04/27', '29.27', '29.812', '29.57', '29.67'), ('2018/04/26', '29.355', '29.897', '29.655', '29.755'), ('2018/04/25', '29.3', '29.842', '29.6', '29.7'), ('2018/04/24', '29.26', '29.802', '29.56', '29.66')]
6. 查詢指定貨幣於指定月份每天收盤價
>>>
twder.specify_month('USD', 2018, 10)
[('2018/10/23', '30.54', '31.23', '30.91', '31.01'), ('2018/10/22', '30.495', '31.185', '30.865', '30.965'), ('2018/10/19', '30.53', '31.22', '30.9', '31'), ('2018/10/18', '30.53', '31.22', '30.9', '31'), ('2018/10/17', '30.41', '31.1', '30.78', '30.88'), ('2018/10/16', '30.485', '31.175', '30.855', '30.955'), ('2018/10/15', '30.5', '31.19', '30.87', '30.97'), ('2018/10/12', '30.44', '31.13', '30.81', '30.91'), ('2018/10/11', '30.68', '31.37', '31.05', '31.15'), ('2018/10/09', '30.54', '31.23', '30.91', '31.01'), ('2018/10/08', '30.505', '31.195', '30.875', '30.975'), ('2018/10/05', '30.405', '31.095', '30.775', '30.875'), ('2018/10/04', '30.36', '31.05', '30.73', '30.83'), ('2018/10/03', '30.24', '30.93', '30.61', '30.71'), ('2018/10/02', '30.245', '30.935', '30.615', '30.715'), ('2018/10/01', '30.11', '30.8', '30.48', '30.58')]
嗯, 這套件很棒.