GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — download stock · enoughbr/StarterLearningPython@7220d48

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • 313.md
  • 3code
    • 31301.py
  • 3images
    • 31301.png
    • 31302.png
  • README.md
  • index.md
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎313.md‎

Copy file name to clipboard
+60Lines changed: 60 additions & 0 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
>神阻挡骄傲的人,赐恩给谦卑的人。(1 PETER 5:5)
2+
3+
#处理股票数据
4+
5+
这段时间某国股市很火爆,不少砖家在分析股市火爆的各种原因,更有不少人看到别人挣钱眼红了,点钞票杀入股市。不过,我还是很淡定的,因为没钱,所以不用担心任何股市风险临到。
6+
7+
但是,为了体现本人也是与时俱进的,就以股票数据为例子,来简要说明pandas和其它模块在处理数据上的应用。
8+
9+
##下载yahoo上的数据
10+
11+
或许你稀奇,为什么要下载yahoo上的股票数据呢?国内网站上不是也有吗?是有。但是,那时某国内的。我喜欢yahoo,因为她曾经吸引我,注意我说的是[www.yahoo.com](http://www.yahoo.com),不是后来被阿里巴巴收购并拆散的那个。
12+
13+
![](./3images/31301.png)
14+
15+
虽然yahoo的世代渐行渐远,但她终究是值得记忆的。所以,我要演示如何下载yahoo财经栏目中的股票数据。
16+
17+
In [1]: import pandas
18+
In [2]: import pandas.io.data
19+
20+
In [3]: sym = "BABA"
21+
22+
In [4]: finace = pandas.io.data.DataReader(sym, "yahoo", start="2014/11/11")
23+
In [5]: print finace.tail(3)
24+
Open High Low Close Volume Adj Close
25+
Date
26+
2015-06-17 86.580002 87.800003 86.480003 86.800003 10206100 86.800003
27+
2015-06-18 86.970001 87.589996 86.320000 86.750000 11652600 86.750000
28+
2015-06-19 86.510002 86.599998 85.169998 85.739998 10207100 85.739998
29+
30+
下载了阿里巴巴的股票数据(自2014年11月11日以来),并且打印最后三条。
31+
32+
##画图
33+
34+
已经得到了一个DataFrame对象,就是前面已经下载并用finace变量引用的对象。
35+
36+
In[6]: import matplotlib.pyplot as plt
37+
In [7]: plt.plot(finace.index, finace["Open"])
38+
Out[]: [<matplotlib.lines.Line2D at 0xa88e5cc>]
39+
40+
In [8]: plt.show()
41+
42+
于是乎出来了下图:
43+
44+
![](./3images/31302.png)
45+
46+
从图中可以看出阿里巴巴的股票自从2014年11月11日到2015年6月19日的股票开盘价变化。看来那个所谓的“光棍节”得到了股市的认可,所以,在此我郑重地建议阿里巴巴要再造一些节日,比如3月3日、4月4日,还好,某国还有农历,阳历用完了用农历。可以维持股票高开高走了。
47+
48+
阿里巴巴的事情,我就不用操心了。
49+
50+
上面指令中的`import matplotlib.pyplot as plt`是个此前没有看到的。`matplotlib`模块是python中绘制二维图形的模块,是最好的模块。本教程在这里展示了它的一个小小地绘图功能,读者就一下看到阿里巴巴“光棍节”的力量,难道还不能说明matplotlib的强悍吗?很可惜,matplotlib的发明者——John Hunter已经于2012年8月28日因病医治无效英年早逝,这真是天妒英才呀。为了缅怀他,请读者访问官方网站:[matplotlib.org](http://matplotlib.org),并认真学习这个模块的使用。
51+
52+
经过上面的操作,读者可以用`dir()`这个以前常用的法宝,来查看finace所引用的DataFrame对象的方法和属性等。只要运用此前不断向大家演示的方法——`dir+help`——就能够对这个对象进行操作,也就是能够对该股票数据进行各种操作。
53+
54+
再次声明,本课程仅仅是稍微演示一下相关操作,如果读者要深入研习,恭请寻找相关的专业书籍资料阅读学习。
55+
56+
------
57+
58+
[总目录](./index.md)&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;[上节:Pandas使用(2)](./312.md)&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;[下节:网络爬虫(1)](./314.md)
59+
60+
如果你认为有必要打赏我,请通过支付宝:**[email protected]**,不胜感激。
Collapse file

‎3code/31301.py‎

Copy file name to clipboard
+37Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#! /usr/bin/env python
2+
# coding = utf-8
3+
4+
import pandas
5+
import pandas.io.data
6+
import datetime
7+
import urllib2
8+
import csv
9+
10+
YAHOO = "http://download.finance.yahoo.com/d/quotes.csv?s=%s&f=sd1ohgl1vl1"
11+
12+
def get_quote_today(symbol):
13+
response = urllib2.urlopen(YAHOO % symbol)
14+
reader = csv.reader(response, delimiter=",", quotechar='"')
15+
for row in reader:
16+
if row[0] == symbol:
17+
return row
18+
19+
def main():
20+
symbol = "BABA"
21+
22+
history = pandas.io.data.DataReader(symbol, "yahoo", start="2014/11/11")
23+
print history.tail(3)
24+
25+
today = datetime.date.today()
26+
df = pandas.DataFrame( columns=["Open", "High", "Low", "Close", "Volume", "Adj Close"], dtype=float)
27+
28+
row = get_quote_today(symbol)
29+
df.ix[0] = map(float, row[2:])
30+
31+
history = history.append(df)
32+
33+
print "today is %s" % today
34+
print history.tail(2)
35+
36+
if __name__ == "__main__":
37+
main()
Collapse file

‎3images/31301.png‎

Copy file name to clipboard
4.44 KB
  • Display the source diff
  • Display the rich diff
Loading
Collapse file

‎3images/31302.png‎

Copy file name to clipboard
41.2 KB
  • Display the source diff
  • Display the rich diff
Loading
Collapse file

‎README.md‎

Copy file name to clipboardExpand all lines: README.md
+1Lines changed: 1 addition & 0 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@
120120
1. [为计算做准备](./310.md)==>相关模块的安装,ipython notebook
121121
2. [Pandas使用(1)](./311.md)==>Series数据类型:定义和基本属性,DataFrame数据类型:定义和基本属性
122122
3. [Pandas使用(2)](./312.md)==>读取csv文件,excel文件,以及其它格式数据和从数据库加载数据方法
123+
4. [处理股票数据](./313.md)==>下载yahoo股票数据,matplotlib模块绘图
123124

124125
##附:网络文摘
125126

Collapse file

‎index.md‎

Copy file name to clipboardExpand all lines: index.md
+1Lines changed: 1 addition & 0 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@
120120
1. [为计算做准备](./310.md)==>相关模块的安装,ipython notebook
121121
2. [Pandas使用(1)](./311.md)==>Series数据类型:定义和基本属性,DataFrame数据类型:定义和基本属性
122122
3. [Pandas使用(2)](./312.md)==>读取csv文件,excel文件,以及其它格式数据和从数据库加载数据方法
123+
4. [处理股票数据](./313.md)==>下载yahoo股票数据,matplotlib模块绘图
123124

124125
##附:网络文摘
125126

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.