引言: Tushare是业界非常有名的开源数据接口,本文将简要介绍如何安装并使用之。
1. 官网主页
TuShare是一个免费、开源的python财经数据接口包; 摘自官网的信息。 http://tushare.waditu.com/
其兼容和接收了诸多数据源的接口数据,非常赞。
2. 安装步骤
本人桌面环境:Ubuntu 16.04 , python 2.7.12
>> sudo pip install tushare
结果爆出以下错误信息:
bladestone@bladestone-laptop:~$ pip install tushare
Collecting tushare
Downloading http://mirrors.aliyun.com/pypi/packages/da/55/6bf90bc5eebada4a0080d3f13804c4adb555427c7c1fe5bb7dabab43ffb3/tushare-0.6.3.tar.gz (87kB)
100% |████████████████████████████████| 92kB 821kB/s
Complete output from command python setup.py egg_info:
Traceback (most recent call last):
File "", line 1, in
File "/tmp/pip-build-pG2c5z/tushare/setup.py", line 4, in
import tushare
File "/tmp/pip-build-pG2c5z/tushare/tushare/__init__.py", line 98, in
from tushare.trader.trader import TraderAPI
File "/tmp/pip-build-pG2c5z/tushare/tushare/trader/trader.py", line 19, in
from astropy.units import acount
ImportError: No module named astropy.units
基于上述错误信息,确认是缺少astropy.units包,那就安装好了
>> sudo pip install astropy
bladestone@bladestone-laptop:~$ pip install astropy
Collecting astropy
Downloading http://mirrors.aliyun.com/pypi/packages/b1/52/9545d406dc5808a380a418f80b846b2a8e9058cb14f5e7a5a88b67b8feec/astropy-1.2.1-cp27-cp27mu-manylinux1_x86_64.whl (8.9MB)
0% |▏ | 51kB 1.9MB/s eta 0:00:05Exception:
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/pip/basecommand.py", line 215, in main
status = self.run(options, args)
File "/usr/local/lib/python2.7/dist-packages/pip/commands/install.py", line 335, in run
wb.build(autobuilding=True)
File "/usr/local/lib/python2.7/dist-packages/pip/wheel.py", line 749, in build
self.requirement_set.prepare_files(self.finder)
File "/usr/local/lib/python2.7/dist-packages/pip/req/req_set.py", line 380, in prepare_files
ignore_dependencies=self.ignore_dependencies))
File "/usr/local/lib/python2.7/dist-packages/pip/req/req_set.py", line 620, in _prepare_file
session=self.session, hashes=hashes)
File "/usr/local/lib/python2.7/dist-packages/pip/download.py", line 821, in unpack_url
hashes=hashes
File "/usr/local/lib/python2.7/dist-packages/pip/download.py", line 659, in unpack_http_url
hashes)
File "/usr/local/lib/python2.7/dist-packages/pip/download.py", line 882, in _download_http_url
_download_url(resp, link, content_file, hashes)
File "/usr/local/lib/python2.7/dist-packages/pip/download.py", line 603, in _download_url
hashes.check_against_chunks(downloaded_chunks)
File "/usr/local/lib/python2.7/dist-packages/pip/utils/hashes.py", line 46, in check_against_chunks
for chunk in chunks:
File "/usr/local/lib/python2.7/dist-packages/pip/download.py", line 571, in written_chunks
for chunk in chunks:
File "/usr/local/lib/python2.7/dist-packages/pip/utils/ui.py", line 139, in iter
for x in it:
File "/usr/local/lib/python2.7/dist-packages/pip/download.py", line 560, in resp_read
decode_content=False):
File "/usr/local/lib/python2.7/dist-packages/pip/_vendor/requests/packages/urllib3/response.py", line 357, in stream
data = self.read(amt=amt, decode_content=decode_content)
File "/usr/local/lib/python2.7/dist-packages/pip/_vendor/requests/packages/urllib3/response.py", line 324, in read
flush_decoder = True
File "/usr/lib/python2.7/contextlib.py", line 35, in __exit__
self.gen.throw(type, value, traceback)
File "/usr/local/lib/python2.7/dist-packages/pip/_vendor/requests/packages/urllib3/response.py", line 250, in _error_catcher
raise ProtocolError('Connection broken: %r' % e, e)
ProtocolError: ("Connection broken: error(104, 'Connection reset by peer')", error(104, 'Connection reset by peer'))
结果没有成功,接着爆出错误信息,针对错误信息分析之后发现,其是Connection reset by peer. 很明显是网络连接错误。正确的重试姿势应该是重新执行一边或者多次:
[sudo] password for bladestone:
The directory '/home/bladestone/.cache/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
The directory '/home/bladestone/.cache/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Collecting astropy
Downloading http://mirrors.aliyun.com/pypi/packages/b1/52/9545d406dc5808a380a418f80b846b2a8e9058cb14f5e7a5a88b67b8feec/astropy-1.2.1-cp27-cp27mu-manylinux1_x86_64.whl (8.9MB)
100% |████████████████████████████████| 8.9MB 6.3MB/s
Requirement already satisfied: numpy>=1.7.0 in /usr/lib/python2.7/dist-packages (from astropy)
Installing collected packages: astropy
Successfully installed astropy-1.2.1
重新安装tushare:
bladestone@bladestone-laptop:~$ sudo pip install tushare
The directory '/home/bladestone/.cache/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
The directory '/home/bladestone/.cache/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Collecting tushare
Downloading http://mirrors.aliyun.com/pypi/packages/da/55/6bf90bc5eebada4a0080d3f13804c4adb555427c7c1fe5bb7dabab43ffb3/tushare-0.6.3.tar.gz (87kB)
100% |████████████████████████████████| 92kB 937kB/s
Installing collected packages: tushare
Running setup.py install for tushare ... done
Successfully installed tushare-0.6.3
tushare安装成功了。
3. 查看tushare的数据
Tushare提供了多种数据源头,这里我们仅仅查看一下票房数据吧。
In [1]: import tushare as ts
In [2]: df = ts.realtime_boxoffice()
In [3]: df
Out[3]:
BoxOffice Irank MovieName boxPer movieDay sumBoxOffice time
0 4391.05 1 长城 62.42 4 51344.45 2016-12-19 21:24:36
1 961.06 2 血战钢锯岭 13.66 12 25038.84 2016-12-19 21:24:36
2 814.83 3 罗曼蒂克消亡史 11.58 4 7374.05 2016-12-19 21:24:36
3 258.72 4 你的名字。 3.68 18 54498.63 2016-12-19 21:24:36
4 170.56 5 28岁未成年 2.42 11 12197.34 2016-12-19 21:24:36
5 108.45 6 少年 1.54 4 991.12 2016-12-19 21:24:36
6 62.71 7 海洋奇缘 0.89 25 19660.88 2016-12-19 21:24:36
7 62.15 8 神奇动物在哪里 0.88 25 58444.60 2016-12-19 21:24:36
8 58.79 9 萨利机长 0.84 11 5708.11 2016-12-19 21:24:36
9 50.72 10 我在故宫修文物 0.72 4 439.16 2016-12-19 21:24:36
10 95.50 11 其它 1.00 0 0.00 2016-12-19 21:24:36
上述内容告知我们长城还是非常牛x的,上映四天,5.1344亿元,当日票房占比62.42%. 大大的厉害了
4. 总结
Tushare是一个非常赞的开源项目,值得我们常常访问。