Package: bocfx
bocfxAn easy-to-use python package for getting foreign exchange rate from Bank of China (BOC).一个帮你快速获取中国银行外汇牌价的 Python 爬虫（也可作外汇牌价实时查询API)。Author:Bob LeeFeatures特点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()）Contents目录Installation 安装Basic Usage 基本用法As a command-line application 作为命令行程序使用As a python module 作为API使用Advanced Usage 进阶用法As a command-line application 作为命令行程序使用ExampleAs a python module 作为API使用ExampleOption and parameter 参数选择Option 1-f/--fx外汇种类Common foreign currency:ExampleOption 2-s/--sort牌价种类ExampleOption 3-t/--time时间、时间段Option 4-p/--plot画图Option 5-c/--csv导出csv表格Option 6-o/--op导出文件路径设置Option 7-b/--bar开启进度条显示Web API (temporary demo)Limitation 局限性MIT LicenceInstallation安装Generally, one line command can perfect installation.pip3 install bocfxIf yourpiplinks python3, please usepip install bocfxto install bocfx.REMEMBER, Bocfx may not support python2.Basic Usage基本用法 (输出最新的牌价)As a command-line application作为命令行工具使用❯ 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)As a Python module作为 Python 模块导入使用>>> from bocfx import bocfx

>>> output = bocfx('GBP,USD','SE,ASK') # 选择了英镑和美元，现汇卖出价
>>> output # The latest foreign exchange rate | 英镑和美元，现汇卖出价 (最新的牌价)
['879.22', '706.76']Advanced Usage进阶用法As a command-line application作为命令行工具使用时-f| 可选外汇 | FX selection-s| 可选牌价 | Quotation types selection-t| 可选时间 | Time / Period selection-p| 输出波动图 | Output plot-c| 输出csv | Output csv table-o| 指定输出路径 | Set output pathbocfx[-f|--fx][-s|--sort][-t|--time](-p|--plot)(-c|--csv){-o|--op}Example:❯ 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.pngAs a python modulefrombocfximportbocfxbocfx(FX=0,sort=0,time=-1,plot=0,csv=0,pt=0,op='~/bocfx_output')Example:>>> 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']]Option and parameter选项和参数Option 1-f/--fx外汇种类-f/--fxis the BOC foreign excahnge selector. This opinion supportsmulti-parameter.,is the separator of each parameter, e.g.GBP,USDSupported foreign currency:FXISO CodeFull NameAlias英镑GBPGreat Britain PoundUK港币HKDHong Kong DollarHK美元USDUnited States DollarUS/USA瑞士法郎CHFSwiss Franc德国马克DEMDeutsche Mark法国法郎FRFFrench FrancFF新加坡元SGDSingapore Dollar瑞典克朗SEKSwedish Krona丹麦克朗DKKDanish Krone挪威克朗NOKNorwegian Krone日元JPYJapanese YenJP加拿大元CADCanadian DollarCA澳大利亚元AUDAustralian DollarAU欧元EUREuroEU澳门元MOPMacao PatacaMO菲律宾比索PHPPhilippine Peso泰国铢THBThai Baht新西兰元NZDNew Zealand DollarKIWI韩国元WONSouth Korean WonSK卢布RUBRussian RubleRU林吉特MYRMalaysia RinggitSEN新台币NTDNew Taiwan DollarTW西班牙比塞塔ESPSpain Peseta意大利里拉ITLItalian Lira荷兰盾ANGNederlandse Gulden比利时法郎BEFBelgian Franc芬兰马克FIMFinnish Mark印度卢比INRIndian Rupee印尼卢比IDRIndonesian Rupee巴西里亚尔BRLBrasil Rial阿联酋迪拉姆AEDUnited Arab Emirates Dirham南非兰特ZAFSouth African Rand沙特里亚尔SARSaudi Arabian Riyal土耳其里拉TRYYeni Türk LirasıYTLFX, ISO and Alias can be used as values of this option, e.g.英镑,GBP,UKare all correct.This parameter is case-insensitive, e.g.GBP,gBp,UK,ukwill get same output.If not setting this parameter, the default value of parameter will beGBP, EUR, USD, CAD, AUD.Example:~
❯ 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)Option 2-s/--sort牌价种类-s/--sortis the filter of quotation types. This opinion supportsmulti-parameter.,is the separator of each parameter.-s/--sortSESpot Exchange 现汇价BNBanknote 现钞价BIDBid Price 买入价SE,BIDSE_BIDBN,BIDBN_BIDASKAsk Price 卖出价SE,ASKSE_ASKBN,ASKBN_ASKIf not using this option, the default output will include all four sorts.Example:~
❯ 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)Option 3-t/--time时间范围选择-t/--timecan filter the date of foreign exchange quotation.There are 3 ways to write parameter.No.ExampleDefinition定义16Get data from 6 days ago to today6 天之内的全部历史价格22019-02-20Get data from 2019-02-20 to today选定时间到今天的全部历史价格32019-02-20,2019-02-21Get data from 2019-02-20 to 2019-02-21选定时间范围内全部历史价格If not using this option,only one price (the latest quotation price)will be output.Option 4-p/--plot是否生成折线图Using-p/--plotcan generate a line graph with .png format.The default output path is~/bocfx_output/Option 5-c/--csv是否生成csv表格Using-c/--csvcan output data in .csv format.The default output path is~/bocfx_output/Option 6-o/--op自定义输出路径Using-o/--opcan change default output path.Example:-o '~/desktop/example/'Option 7-b/--bar开启进度条显示Using-b/--barcan show loading bar while the tasks are running.Web API (temporary demo) (discarded now)HTTP GET:http://sh.boblee.cn:8080/apis/bocfx?FX=GBP,USD&sort=SE&time=&format=jsonParameterExamplesFX=(Blank = GBP,EUR,USD,CAD,AUD),GBP,GBP,USD,AUDsort=(Blank = ALL four types),SE,BN,ASK,BID,SE,ASK,SE,BID,BN,ASK,BN,BIDtime=(Blank = The latest record),1,2020-03-07,2020-03-07,2020-03-14format=(Blank = json),json,csv,originalDescription of each parameter can be found .每个参数的含义可参见上方的介绍.Limitation局限性This package is no-database design.All data are captured fromhttp://srh.bankofchina.com/search/whpj/search.jspin real time.Thus, the speed of spider will be strongly influenced by the connectation quality!本脚本会受 中国银行官网接口速度 的影响！MIT LicenceCopyright (c) 2019 The Python Packaging AuthorityPermission 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.