An easy-to-use python package for getting foreign exchange rate from Bank of China (BOC).
一个帮你快速获取中国银行外汇牌价的 Python 爬虫(也可作外汇牌价实时查询API)。
Author: Bob Lee
- Multithreading 多线程
- As a command-line application 支持命令行应用模式
- As a python module 支持模块导入模式(import到你自己的项目里就能作API了)
- Foreign exchange selection 支持多种外汇币种同时输出(默认是英、欧、美、加、澳)
- Foreign exchange quotation filter 支持外汇牌价种类同时输出(买入、卖出、现钞、现汇、中行折算价)
- Date selection (Realtime/History) 支持仅输出最新牌价 或 输出指定时间段内全部牌价变动
- Line graph generation 支持生成历史波动曲线
- .csv generation 支持导出csv格式表格文件
- Set output path 支持导出到指定路径
- Show or close loading bar 支持显示或关闭进度条
- Export to database 支持导出到数据库(可使用Pandas的df.to_sql())
- Installation 安装
- Basic Usage 基本用法
- Advanced Usage 进阶用法
- Option and parameter 参数选择
- Web API (temporary demo)
- Limitation 局限性
- MIT Licence
Generally, one line command can perfect installation.
pip3 install bocfx
If your pip
links python3, please use pip install bocfx
to install bocfx.
REMEMBER, Bocfx may not support python2.
❯ bocfx
+-----+--------+--------+--------+--------+----------+---------------------+
| 5 | SE_BID | BN_BID | SE_ASK | BN_ASK | BOC_CONV | Time |
+-----+--------+--------+--------+--------+----------+---------------------+
| GBP | 873.01 | 845.88 | 879.44 | 883.32 | 874.54 | 2023-05-23 23:09:35 |
| EUR | 757.61 | 734.07 | 763.19 | 765.65 | 760.31 | 2023-05-23 23:09:35 |
| USD | 703.77 | 698.05 | 706.76 | 706.76 | 703.26 | 2023-05-23 23:09:35 |
| CAD | 520.43 | 504 | 524.27 | 526.58 | 520.83 | 2023-05-23 23:09:35 |
| AUD | 465.18 | 450.73 | 468.6 | 470.68 | 467.72 | 2023-05-23 23:09:35 |
+-----+--------+--------+--------+--------+----------+---------------------+
(SE = Spot Exchange, BN = Banknote)
❯ bocfx -f USD
+-----+--------+--------+--------+--------+----------+---------------------+
| 1 | SE_BID | BN_BID | SE_ASK | BN_ASK | BOC_CONV | Time |
+-----+--------+--------+--------+--------+----------+---------------------+
| USD | 703.77 | 698.05 | 706.76 | 706.76 | 703.26 | 2023-05-23 23:25:12 |
+-----+--------+--------+--------+--------+----------+---------------------+
(SE = Spot Exchange, BN = Banknote)
>>> from bocfx import bocfx
>>> output = bocfx('GBP,USD','SE,ASK') # 选择了英镑和美元,现汇卖出价
>>> output # The latest foreign exchange rate | 英镑和美元,现汇卖出价 (最新的牌价)
['879.22', '706.76']
-f
| 可选外汇 | FX selection
-s
| 可选牌价 | Quotation types selection
-t
| 可选时间 | Time / Period selection
-p
| 输出波动图 | Output plot
-c
| 输出csv | Output csv table
-o
| 指定输出路径 | Set output path
bocfx [-f|--fx] [-s|--sort] [-t|--time] (-p|--plot) (-c|--csv) {-o|--op}
❯ bocfx -f GBP,USD,AUD -s ASK -t 30 -p -c -o '~/desktop/output'
100%|█████████████████████████████████████████████| 3/3 [00:03<00:00, 1.08s/it]
100%|████████████████████████████████████████| 199/199 [00:00<00:00, 422.62it/s]
+-----+--------+--------+---------------------+
| 3 | SE_ASK | BN_ASK | Time |
+-----+--------+--------+---------------------+
| GBP | 880.27 | 882.41 | 2019-02-24 10:30:00 |
| USD | 673.08 | 673.08 | 2019-02-24 10:30:00 |
| AUD | 480.65 | 481.82 | 2019-02-24 10:30:00 |
| GBP | 880.27 | 882.41 | 2019-02-24 05:30:00 |
| USD | 673.08 | 673.08 | 2019-02-24 05:30:00 |
| AUD | 480.65 | 481.82 | 2019-02-24 05:30:00 |
...
| GBP | 889.64 | 891.8 | 2019-01-25 00:00:05 |
| USD | 680.4 | 680.4 | 2019-01-25 00:00:05 |
| AUD | 484.37 | 485.56 | 2019-01-25 00:00:05 |
+-----+--------+--------+---------------------+
(SE = Spot Exchange, BN = Banknote)
.csv has already saved to /Users/bob/desktop/output/[GBP+USD+AUD]SE_ASK+BN_ASK_2019-01-25_2019-02-24.csv
Plot has already saved to /Users/bob/desktop/output/[GBP+USD+AUD]SE_ASK+BN_ASK_2019-01-25_2019-02-24.png
from bocfx import bocfx
bocfx(FX=0, sort=0, time=-1, plot=0, csv=0, pt=0, op='~/bocfx_output')
>>> from bocfx import bocfx
# Equivalent command
# example01 = bocfx(FX='GBP,USD', sort='SE', time=1)
>>> example01 = bocfx('GBP,USD','SE',1)
100%|█████████████████████████████████████████████| 2/2 [00:01<00:00, 1.49it/s]
>>> type(example01)
<class 'list'>
>>> print(example01)
[[2, 'SE_BID', 'SE_ASK', 'Time'], ['USD', '669.74', '672.58', '2019-02-25 09:25:23'], ['GBP', '874.18', '880.62', '2019-02-25 09:25:13'], ... , ['GBP', '873.83', '880.27', '2019-02-24 00:00:05'], ['USD', '670.24', '673.08', '2019-02-24 00:00:05']]
>>> from bocfx import bocfx
# Equivalent command
# example02 = bocfx(FX='GBP,USD', sort='SE', time='2018-08-01,2018-08-02', plot=1, csv=1, pt=0, op='~/desktop/example/')
>>> example02 = bocfx('GBP,USD','SE','2018-08-01,2018-08-02',1,1,0,op='~/desktop/example/')
100%|█████████████████████████████████████████████| 2/2 [00:02<00:00, 1.05it/s]
100%|██████████████████████████████████████████| 26/26 [00:00<00:00, 134.46it/s]
.csv has already saved to /Users/bob/desktop/example/[GBP+USD]SE_BID+SE_ASK_2018-08-01_2018-08-02.csv
Plot has already saved to /Users/bob/desktop/example/[GBP+USD]SE_BID+SE_ASK_2018-08-01_2018-08-02.png
>>> type(example02)
<class 'list'>
>>> print(example02)
[[2, 'SE_BID', 'SE_ASK', 'Time'], ['GBP', '888.85', '895.4', '2018-08-02 23:19:36'], ['USD', '683.01', '685.91', '2018-08-02 23:19:36'], ... , ['GBP', '891.33', '897.89', '2018-08-01 00:00:05'], ['USD', '680.22', '683.1', '2018-08-01 00:00:05']]
-f
/--fx
is the BOC foreign excahnge selector. This opinion supports multi-parameter.
,
is the separator of each parameter, e.g. GBP,USD
FX | ISO Code | Full Name | Alias |
---|---|---|---|
英镑 |
GBP |
Great Britain Pound | UK |
港币 |
HKD |
Hong Kong Dollar | HK |
美元 |
USD |
United States Dollar | US /USA |
瑞士法郎 |
CHF |
Swiss Franc | |
德国马克 |
DEM |
Deutsche Mark | |
法国法郎 |
FRF |
French Franc | FF |
新加坡元 |
SGD |
Singapore Dollar | |
瑞典克朗 |
SEK |
Swedish Krona | |
丹麦克朗 |
DKK |
Danish Krone | |
挪威克朗 |
NOK |
Norwegian Krone | |
日元 |
JPY |
Japanese Yen | JP |
加拿大元 |
CAD |
Canadian Dollar | CA |
澳大利亚元 |
AUD |
Australian Dollar | AU |
欧元 |
EUR |
Euro | EU |
澳门元 |
MOP |
Macao Pataca | MO |
菲律宾比索 |
PHP |
Philippine Peso | |
泰国铢 |
THB |
Thai Baht | |
新西兰元 |
NZD |
New Zealand Dollar | KIWI |
韩国元 |
WON |
South Korean Won | SK |
卢布 |
RUB |
Russian Ruble | RU |
林吉特 |
MYR |
Malaysia Ringgit | SEN |
新台币 |
NTD |
New Taiwan Dollar | TW |
西班牙比塞塔 |
ESP |
Spain Peseta | |
意大利里拉 |
ITL |
Italian Lira | |
荷兰盾 |
ANG |
Nederlandse Gulden | |
比利时法郎 |
BEF |
Belgian Franc | |
芬兰马克 |
FIM |
Finnish Mark | |
印度卢比 |
INR |
Indian Rupee | |
印尼卢比 |
IDR |
Indonesian Rupee | |
巴西里亚尔 |
BRL |
Brasil Rial | |
阿联酋迪拉姆 |
AED |
United Arab Emirates Dirham | |
南非兰特 |
ZAF |
South African Rand | |
沙特里亚尔 |
SAR |
Saudi Arabian Riyal | |
土耳其里拉 |
TRY |
Yeni Türk Lirası | YTL |
FX, ISO and Alias can be used as values of this option, e.g. 英镑
, GBP
, UK
are all correct.
This parameter is case-insensitive, e.g. GBP
, gBp
, UK
, uk
will get same output.
If not setting this parameter, the default value of parameter will be GBP, EUR, USD, CAD, AUD
.
~
❯ bocfx
100%|████████████████████████████████████████████| 5/5 [00:00<00:00, 612.38it/s]
+-----+--------+--------+--------+--------+---------------------+
| 5 | SE_BID | BN_BID | SE_ASK | BN_ASK | Time |
+-----+--------+--------+--------+--------+---------------------+
| GBP | 874.37 | 847.2 | 880.81 | 882.95 | 2019-02-25 07:28:17 |
| EUR | 759.05 | 735.47 | 764.65 | 766.35 | 2019-02-25 07:28:17 |
| USD | 670.24 | 664.79 | 673.08 | 673.08 | 2019-02-25 07:28:17 |
| CAD | 509.47 | 493.39 | 513.23 | 514.47 | 2019-02-25 07:28:17 |
| AUD | 478.68 | 463.81 | 482.21 | 483.38 | 2019-02-25 07:28:17 |
+-----+--------+--------+--------+--------+---------------------+
(SE = Spot Exchange, BN = Banknote)
~
❯ bocfx -f GBP,USD
100%|███████████████████████████████████████████| 2/2 [00:00<00:00, 1818.08it/s]
+-----+--------+--------+--------+--------+---------------------+
| 2 | SE_BID | BN_BID | SE_ASK | BN_ASK | Time |
+-----+--------+--------+--------+--------+---------------------+
| GBP | 874.37 | 847.2 | 880.81 | 882.95 | 2019-02-25 07:28:17 |
| USD | 670.24 | 664.79 | 673.08 | 673.08 | 2019-02-25 07:28:17 |
+-----+--------+--------+--------+--------+---------------------+
(SE = Spot Exchange, BN = Banknote)
-s
/--sort
is the filter of quotation types. This opinion supports multi-parameter.
,
is the separator of each parameter.
-s /--sort |
SE Spot Exchange 现汇价 |
BN Banknote 现钞价 |
---|---|---|
BID Bid Price 买入价 |
SE,BID SE_BID |
BN,BID BN_BID |
ASK Ask Price 卖出价 |
SE,ASK SE_ASK |
BN,ASK BN_ASK |
If not using this option, the default output will include all four sorts.
~
❯ bocfx -f GBP,USD -s ASK
100%|████████████████████████████████████████████| 2/2 [00:00<00:00, 443.63it/s]
+-----+--------+--------+---------------------+
| 2 | SE_ASK | BN_ASK | Time |
+-----+--------+--------+---------------------+
| GBP | 880.81 | 882.95 | 2019-02-25 07:28:17 |
| USD | 673.08 | 673.08 | 2019-02-25 08:35:54 |
+-----+--------+--------+---------------------+
(SE = Spot Exchange, BN = Banknote)
~
❯ bocfx -f GBP,USD -s SE,BID
100%|████████████████████████████████████████████| 2/2 [00:00<00:00, 443.26it/s]
+-----+--------+---------------------+
| 2 | SE_BID | Time |
+-----+--------+---------------------+
| GBP | 874.57 | 2019-02-25 08:35:54 |
| USD | 670.24 | 2019-02-25 08:35:54 |
+-----+--------+---------------------+
(SE = Spot Exchange, BN = Banknote)
-t
/--time
can filter the date of foreign exchange quotation.
There are 3 ways to write parameter.
No. | Example | Definition | 定义 |
---|---|---|---|
1 | 6 |
Get data from 6 days ago to today | 6 天之内的全部历史价格 |
2 | 2019-02-20 |
Get data from 2019-02-20 to today | 选定时间到今天的全部历史价格 |
3 | 2019-02-20,2019-02-21 |
Get data from 2019-02-20 to 2019-02-21 | 选定时间范围内全部历史价格 |
If not using this option, only one price (the latest quotation price) will be output.
Using -p
/--plot
can generate a line graph with .png format.
The default output path is ~/bocfx_output/
Using -c
/--csv
can output data in .csv format.
The default output path is ~/bocfx_output/
Using -o
/--op
can change default output path.
Example: -o '~/desktop/example/'
Using -b
/--bar
can show loading bar while the tasks are running.
HTTP GET: http://sh.boblee.cn:8080/apis/bocfx?FX=GBP,USD&sort=SE&time=&format=json
Parameter | Examples |
---|---|
FX= | (Blank = GBP,EUR,USD,CAD,AUD) , GBP , GBP,USD,AUD |
sort= | (Blank = ALL four types) , SE , BN , ASK , BID , SE,ASK , SE,BID , BN,ASK , BN,BID |
time= | (Blank = The latest record) , 1 , 2020-03-07 , 2020-03-07,2020-03-14 |
format= | (Blank = json) , json , csv , original |
Description of each parameter can be found .
每个参数的含义可参见上方的介绍.
This package is no-database design.
All data are captured from http://srh.bankofchina.com/search/whpj/search.jsp in real time.
Thus, the speed of spider will be strongly influenced by the connectation quality!
本脚本会受 中国银行官网接口速度 的影响!
Copyright (c) 2019 The Python Packaging Authority
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.