forked from garydai/stock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
draw.py
150 lines (127 loc) · 3.22 KB
/
draw.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#encoding:utf-8
import os
import matplotlib.pyplot as plt
import struct
from time import *
from glob import glob
import matplotlib.pyplot as pl
import re,urllib2
from bs4 import BeautifulSoup
from matplotlib.ticker import MultipleLocator, FuncFormatter
file_names = glob('score*')
#file_names = glob('industry*')
file_names.sort()
stock = {}
xlabel = file_names[0]
for fname in file_names:
f = open(fname, 'r')
print fname
summ = 0
stock_t = {}
while 1:
line = f.readline()
#print line
if not line:
break
array = line[:-1].split('%')
a = array[0].decode('utf-8')
#print array[2]
#if stock_t.has_key(a):
# stock_t[a].append(int(array[2]))
#else:
stock_t[a] = int(array[2])
#print line
summ += int(array[2])
for key in stock_t:
if stock.has_key(key):
stock[key].append(stock_t[key]/float(summ))
else:
stock[key] = [stock_t[key]/float(summ)]
#print stock[u'中信证券']
#url = 'http://quotes.money.163.com/trade/lsjysj_'+ '601800'+'.html?year=2014&season=4'
url = 'http://quotes.money.163.com/trade/lsjysj_'+ '600030'+'.html?year=2014&season=4'
print url
#print("股票代码:" + stock_num)
headers = {"User-Agent":"Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6"}
req = urllib2.Request( url, headers = headers)
try:
content = urllib2.urlopen(req).read()
except Exception,e:
print e
#return 0
soup = BeautifulSoup(content)
xlabel = xlabel[5:-4]
xlabel = xlabel[8:-4]
table = soup.find('table',class_='table_bg001 border_box limit_sale')
tr = table.findAll('tr')
#print td
web = {}
for i in range(1, len(tr)):
td = tr[i].findAll('td')
web[td[0].contents[0]] = (td[4].contents[0])
url = 'http://quotes.money.163.com/trade/lsjysj_'+ '600030'+'.html'
print url
#print("股票代码:" + stock_num)
headers = {"User-Agent":"Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6"}
req = urllib2.Request( url, headers = headers)
try:
content = urllib2.urlopen(req).read()
except Exception,e:
print e
#return 0
soup = BeautifulSoup(content)
xlabel = xlabel[5:-4]
xlabel = xlabel[8:-4]
table = soup.find('table',class_='table_bg001 border_box limit_sale')
tr = table.findAll('tr')
for i in range(1, len(tr)):
td = tr[i].findAll('td')
web[td[0].contents[0]] = (td[4].contents[0])
print web
y2=[]
for fname in file_names:
nm = fname[5:-4]
nm = fname[5:-4]
if web.has_key(nm.decode('utf-8')):
y2.append(float(web[nm.decode('utf-8')]))
else:
y2.append(0)
print y2
fig = plt.figure()
f = fig.add_subplot(111)
f2 = fig.add_subplot(111)
#ax = plt.gca()
#ax.xaxis.set_major_locator( MultipleLocator(1) )
#for key in stock:
key = u'中信证券'
#key = u'中国交建'
x = range(len(stock[key]))
print stock[key]
y = stock[key]
yy1 = []
yy2 = []
for i in range(len(stock[key])):
if y[i] != 0 and y2[i] !=0:
yy1.append(y[i])
yy2.append(y2[i])
print yy2
#normal
minn = min(yy1)
maxx = max(yy1)
yy1 = [ (t - minn) / (maxx - minn) for t in yy1 ]
minn = min(yy2)
maxx = max(yy2)
yy2 = [ (t - minn) / (maxx - minn) for t in yy2 ]
x = range(len(yy1))
#t =float(sum(y))
#y = [ a / t for a in y]
#print t
print x, y
plt.xlabel(xlabel)
print xlabel
f.plot(x, yy1, color = 'red')
f2.plot(x, yy2)
plt.savefig("1.png")
#
plt.show()
#break