-
Notifications
You must be signed in to change notification settings - Fork 0
/
getAllStock.py
77 lines (69 loc) · 2.26 KB
/
getAllStock.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
# -*- coding: utf-8 -*-
import sys
from getUrl import getUrl
from getUrl import getUrl2
import simplejson as json
from bs4 import BeautifulSoup
import time
import MySQLdb as mdb
import dbconfig
reload(sys)
sys.setdefaultencoding( "utf-8" )
page = 1
ret = getUrl2("http://money.finance.sina.com.cn/d/api/openapi_proxy.php/?__s=[[\"hq\",\"hs_a\",\"\",0,%d,40]]"%(page));
#print ret
retjson = json.JSONDecoder().decode(ret)
stockCount = retjson[0]["count"]
print stockCount
pages = stockCount / 40 + 1
conn = dbconfig.connectDB()
cur = conn.cursor()
cur.execute("SET NAMES utf8")
cur.execute("SET CHARACTER_SET_CLIENT=utf8")
cur.execute("SET CHARACTER_SET_RESULTS=utf8")
values = ""
curDay = time.strftime('%Y-%m-%d',time.localtime(time.time() ))
curDay += " 16:00:00"
timeArray = time.strptime(curDay, "%Y-%m-%d %H:%M:%S")
curTime = int(time.mktime(timeArray))
curTimestamp = str(curTime)
while page <= pages:
ret = getUrl2("http://money.finance.sina.com.cn/d/api/openapi_proxy.php/?__s=[[\"hq\",\"hs_a\",\"\",0,%d,40]]"%(page));
#print ret
retjson = json.JSONDecoder().decode(ret)
num = len(retjson[0]["items"])
print num
stockIdStr = ""
for element in retjson[0]["items"]:
values +="("
i = 0
while i < 22 :
values += "\'"+ str(element[i]) +"\',"
i = i + 1
pe = float(element[15])
pe_d = float(element[16])
pb = float(element[18])
print element[2]
if pb <= 0 or pe <=0 :
gvi = str(0)
else:
gvi = str( (1.0/ pb) * (1 + (pb/pe) )**5)
if pb <= 0 or pe_d <=0 :
gvi_d = str(0)
else:
gvi_d = str( (1.0/ pb) * (1 + (pb/pe_d) )**5)
#print str(float(element[16]) *(float(1 + element[18])**5))
print gvi
print "======"
values += gvi
values += ","
values += gvi_d
values += ","
values += "from_unixtime(%s)"%curTimestamp
values += "),"
page = page + 1
values = values[0: -1]
sql = "insert into stockDaliy(symbol,code,name,trade,pricechange,changepercent,buy,sell,settlement,open,high,low,volume,amount,ticktime,per,per_d,nta,pb,mktcap,nmc,turnoverratio,gvi,gvi_d,createTime) values %s"%(values);
#print sql
cur.execute(sql)
conn.commit()