/
DataCollecter.py
182 lines (155 loc) · 6.1 KB
/
DataCollecter.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import urllib2
import time
import string
from sgmllib import SGMLParser
from EastmoneyContent import *
from DBManager import *
from URLBuilder import *
import os
class DataCollecter():
def __init__(self):
self._DBHandle = DBManager()
self._DBCur = self._DBHandle.getDBCur()
self._DBCon = self._DBHandle.getDBCon()
self._URLInstance = URLBuilder()
self._code = ""
self._Component = ""
self._fileSet = set()
pass
def UpdateStocks(self, mark, IDHead, IDTail):
try:
self._DBCur.execute('''CREATE TABLE collections ''' + '''
(code text, recordtype text, date text, holders real, holderschangeper real, averagehold real, averageholdchangeper real,
scr text, price real, averageamount real, top10hold real, top10outstandinghold real)''')
except:
pass
countTmp = 0
for x in range(IDHead,IDTail):
urlToGet = self._URLInstance.GetURL(self.BuildCode(mark, x, 1))
try:
content = urllib2.urlopen(urlToGet).read()
time.sleep(3)
countTmp += 1
if countTmp == 50:
time.sleep(6)
countTmp = 0
except:
continue
self._ContentInstance = EastmoneyContent()
self._ContentInstance.feed(content)
self._ContentInstance.handle_listmatrix(self.BuildCode(mark, x, 0), "F10")
#-----------------begin sqlite----------------------------
if self._ContentInstance.listmatrix != []:
for iteminlistmatrix in self._ContentInstance.listmatrix:
codeTmp = iteminlistmatrix[0]
dateTmp = iteminlistmatrix[2]
executeSQL = 'select * from collections where date = \"' + dateTmp + '\" and code = \"' + codeTmp + '\"'
self._DBCur.execute(executeSQL)
SQLTmp = self._DBCur.fetchall()
if SQLTmp == []:
executeSQL = 'INSERT INTO collections VALUES (?,?,?,?,?,?,?,?,?,?,?,?)'
self._DBCur.execute(executeSQL, iteminlistmatrix)
self._DBCon.commit()
print " done\n"
self._DBCon.commit()
else:
print " has none\n"
def BuildCode(self, mark, x, mix):
if mark == "sh":
self._code = "6" + str(x)[-5:]
self._Component = mark + self._code
if mark == "sz":
self._code = str(x)[-6:]
self._Component = mark + self._code
if mix == 1:
return self._Component
if mix == 0:
return self._code
def UpdateStockname(self, filename):
x = 0
for line in open(filename):
if x == 0 :
x += 1
continue
x += 1
linesplited = line.split('\t')
executeSQL = ' update collections set name = \"' + linesplited[2] + '\" where code = ' + linesplited[1] + ' '
self._DBCur.execute(executeSQL)
self._DBCon.commit()
pass
def NewTab(self, filename):
x = 0
for line in open(filename):
firstline = []
pricenow = []
if x == 0 :
x += 1
firstline = line.split('\t')
try:
self._DBCur.execute('''CREATE TABLE price ''' + '''(code text, newprice real, toplevel real, toplimitdays real)''')
except:
pass
continue
x += 1
linesplited = line.split('\t')
executeSQL = 'INSERT INTO price VALUES (?,?,?,?)'
pricenow.append(linesplited[1][1:7])
pricenow.append(linesplited[7])
pricenow.append(linesplited[10])
pricenow.append(linesplited[8])
self._DBCur.execute(executeSQL, pricenow)
self._DBCon.commit()
pass
def UpdateStocksHtmls(self, mark, IDList):
countTmp = 0
#if not os.path.isdir(mark):
# os.makedirs(mark)
path = "K:\\codes\\ApplicationStock\\ApplicationStocks\\sh"
os.chdir(path)
if len(self._fileSet) == 0:
for root, dirs, files in os.walk( path ):
for fn in files:
self._fileSet.add(fn)
for x in IDList:
'''
for root, dirs, files in os.walk( path ):
for fn in files:
if fn not in self._fileSet:
self._fileSet.add(fn)
'''
urlToGet = self._URLInstance.GetURL(self.BuildCode(mark, x, 1))
if urlToGet[-6:] in self._fileSet:
continue
if os.path.exists(urlToGet[-6:]) == True:
continue
try:
content = urllib2.urlopen(urlToGet).read()
time.sleep(1)
countTmp += 1
if countTmp == 50:
time.sleep(6)
countTmp = 0
except:
continue
fileobj = open(urlToGet[-6:], "wb")
fileobj.write(content)
fileobj.close()
def UpdateStocksHtmlsFinance(self, mark, IDHead, IDTail):
countTmp = 0
if not os.path.isdir(mark + "finance"):
os.makedirs(mark + "finance")
os.chdir( "./" + mark + "finance" + "/")
for x in range(IDHead,IDTail):
urlToGet = self._URLInstance.GetURL(self.BuildCode(mark, x, 1))
try:
content = urllib2.urlopen(urlToGet).read()
time.sleep(0)
countTmp += 1
if countTmp == 50:
time.sleep(6)
countTmp = 0
except:
continue
fileobj = open(urlToGet[-8:], "wb")
fileobj.write(content)
fileobj.close()