14.1.1 ftplib
# 将文件传输到FTP服务器 import ftplib import numpy as np ftp = ftplib.FTP('quant-platform.com') ftp.login(user='***', passwd='***') # 生成一个包含随机数据的NumPy ndarray 对象 , 保存到磁盘作为传输文件 np.save('./data/array', np.random.standard_normal((100, 100))) f = open('./data/array.npy', 'r') # 二进制传输, 写人服务器 ftp.storbinary('STOR array.npy', f) # 查看一下FTP服务器的目录 ftp.retrlines('LIST') # 反向操作,读取一个远程文件并保存到磁盘 f = open('./data/array_ftp.npy', 'wb').write ftp.retrbinary('RETR array.npy', f) # 现在不再需要将文件保存在服务器了,可以删除它: ftp.delete('array.npy') ftp.retrlines('LIST') # 关闭FTP服务器连接 ftp.close() # ftpllb可以通过函数FTP_TLS安全连接到FTP服务器。 # 一旦建立了这样的安全连接,所有其他操作都保持不变 ftps = ftplib.FTP_TLS('quant-plltform.com') ftps.login(user='***', passwd='***') ftps.prot_p() ftp.retrlines('LIST') ftps.close() # 因为用的比较少,所以没有测试,了解思路就好
14.1.2 http.client
import http.client as hc http = hc.HTTPConnection('www.baidu.com') http.request('GET', '/') resp = http.getresponse() resp.status, resp.reason # (200, 'OK') content = resp.read() content[:100] # b'<!DOCTYPE html><!--STATUS OK-->\r\n<html>\r\n<head>\r\n\t<meta http-equiv="content-type" content="text/html' http.close()
14.1.3 requests
import requests url = 'http://www.baidu.com' response = requests.get(url) data = response.content
import tushare as ts # 中国卫星(600118)股票2014年以来的股票走势 data = ts.get_k_data('600118', '2014-01-01', ktype='D') data.plot(x='date', y='close')
import bokeh.plotting as bp bp.output_file('E:/project/MyPyTest/python_for_finance/data/600118.html', title='Bokeh Example (Static)') p = bp.figure(title="line", plot_width=800, plot_height=600) p.line( x=data['date'].astype('datetime64[ns]').values.tolist(), # x coordinates y=data['close'].values.tolist(), # y coordinates color='#0066cc', # set acolor for the line legend='中国卫星' ) bp.show(p)
图像自动在浏览器中打开
更多关于 bokeh 的例子 参考: https://bokeh.pydata.org/en/latest/docs/gallery.html
bokeh 还包括一些交互性元素(“工具”):
pan
支持图表平移(就像用摄影机摇拍);也就是说, 相对于固定的画框移动图表(包括x和y 坐标)
wheel zoom
使用鼠标滚轮实现图像缩放
bοx zoom
用鼠标标记一个方框实现缩放
reset
重置图表的原始/默认视图
save
生成可以保存为PNG格式的图表静态(位图)版本
具体的例子参见 https://bokeh.pydata.org/en/latest/docs/gallery.html
中的 stocks 例子
在这里我们用随机数代替实时数据,创建一个 bokeh_test.py:
import bokeh.plotting as bp from bokeh.layouts import column from bokeh.models import Button import time import random p = bp.figure(x_range=(0, 100), y_range=(0, 100)) r = p.line( x=[], # x coordinates y=[], # y coordinates color='#0066cc', # set acolor for the line legend='random' ) ds = r.data_source clicked = False def callback(): global clicked if (clicked): return else: clicked = True i = 0 start = time.time() # run for 60 seconds while (time.time() - start) < 60: new_data = dict() y = random.randint(0, 100) new_data['x'] = ds.data['x'] + [i] new_data['y'] = ds.data['y'] + [y] ds.data = new_data i += 1 time.sleep(1) button = Button(label="Start") button.on_click(callback) bp.curdoc().add_root(column(button, p))
启动 bokeh服务
E:\project\MyPyTest\python_for_finance\data>
关于 bokeh服务 参考:http://bokeh.pydata.org/en/latest/docs/user_guide/server.html
启动后浏览器会自动打开连接: http://localhost:5006/bokeh_test
点击网页中的 “Start” 按钮,程序就开始显示实时数字了:
如果想要展示真实的数据,把while循环中的代码变成获取股票、汇率等实时数据的 api 代码即可。