-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathget_last_5.py
More file actions
33 lines (28 loc) · 845 Bytes
/
get_last_5.py
File metadata and controls
33 lines (28 loc) · 845 Bytes
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
# -*- coding: utf-8 -*-
#!/usr/local/bin/python`
import urllib
import re
import math
import sys
reload(sys)
sys.setdefaultencoding("utf-8")
f = open('stock_code', 'r')
for code in f.readlines():
link = 'http://hq.sinajs.cn/list=%s' % (code)
f = urllib.urlopen(link)
myfile = f.read()
myfile = myfile.decode('gbk')
p = re.compile('(s[hz][\d]+)[\w\W]+\"([\W\w]+)\"')
m = p.search(myfile)
if not m:
print 'can not find stock information for %s' % (code)
continue
stock_code = m.group(1)
ns = m.group(2).split(',')
stock_name = ns[0]
price_start = ns[1].encode('utf-8')
price_end = ns[3].encode('utf-8')
#print stock_name, stock_code, price_start, price_end
if (math.fabs(float(price_end) - float(price_start)) < 0.02):
print stock_name, stock_code, price_start, price_end
f.close()