Repository navigation
Expand file tree
/
Copy pathTimeSeqSpider.py
More file actions
170 lines (141 loc) · 5.29 KB
/
Copy pathTimeSeqSpider.py
File metadata and controls
170 lines (141 loc) · 5.29 KB
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
import json
import requests
import time
def getBaseInfo(code):
null=""
fromData={'mergerMark':'sysapi1068','paramStr':'scode='+code}
page=requests.post('http://www.cninfo.com.cn/data/project/commonInterface', data = fromData)
BaseInfoDic={}
BaseInfoDic['code']=code
try:
BaseInfo = json.loads(page.text[1:-1])
BaseInfoDic['成立时间']=BaseInfo['F001D']
BaseInfoDic['上市日期']=BaseInfo['F002D']
BaseInfoDic['所属行业']=BaseInfo['F010V']
BaseInfoDic['上市板块']=BaseInfo['F011V']
print('succeed in processing ',code)
except:
print('fail to process ',code)
finally:
return BaseInfoDic
def getZhiBiao(code):
null=""
fromData={'mergerMark':'sysapi1074','paramStr':'scode='+code+';rtype=1'}
page=requests.post('http://www.cninfo.com.cn/data/project/commonInterface', data = fromData)
ZhiBiaoDic={}
ZhiBiaoDic['code']=code
try:
ZhiBiaoList=eval(page.text)
for ZhiBiao in ZhiBiaoList:
ZhiBiaoDic[ZhiBiao['index']]={}
year=2011
for i in range(10):
if str(year) in ZhiBiao.keys():
ZhiBiaoDic[ZhiBiao['index']][str(year)]=ZhiBiao[str(year)]
else:
ZhiBiaoDic[ZhiBiao['index']][str(year)]=''
year+=1
print('succeed in processing ',code)
except:
print('fail to process ',code)
finally:
return ZhiBiaoDic
def getLiRun(code):
null=''
fromData={'mergerMark':'sysapi1075','paramStr':'scode='+code+';rtype=1;sign=2'}
page=requests.post('http://www.cninfo.com.cn/data/project/commonInterface', data = fromData)
LiRunDic={}
LiRunDic['code']=code
try:
LiRunList=eval(page.text)
for LiRun in LiRunList:
LiRunDic[LiRun['index']]={}
year=2011
for i in range(10):
if str(year) in LiRun.keys():
LiRunDic[LiRun['index']][str(year)]=LiRun[str(year)]
else:
LiRunDic[LiRun['index']][str(year)]=''
year+=1
print('succeed in processing ',code)
except:
feature=['营业收入','营业成本','营业利润','利润总额','所得税','净利润']
for fea in feature:
LiRunDic[fea]={}
year=2011
for i in range(10):
LiRunDic[fea][str(year)]=''
year+=1
print('fail to process ',code)
finally:
return LiRunDic
def getXianJin(code):
null=''
fromData={'mergerMark':'sysapi1076','paramStr':'scode='+code+';rtype=1;sign=2'}
page=requests.post('http://www.cninfo.com.cn/data/project/commonInterface', data = fromData)
XianJinDic={}
XianJinDic['code']=code
try:
XianJinList=eval(page.text)
for XianJin in XianJinList:
XianJinDic[XianJin['index']]={}
year=2011
for i in range(10):
if str(year) in XianJin.keys():
XianJinDic[XianJin['index']][str(year)]=XianJin[str(year)]
else:
XianJinDic[XianJin['index']][str(year)]=''
year+=1
print('succeed in processing ',code)
except:
feature=['经营活动产生的现金流量净额','投资活动产生的现金流量净额','筹资活动产生的现金流量净额']
for fea in feature:
XianJinDic[fea]={}
year=2011
for i in range(10):
XianJinDic[fea][str(year)]=''
year+=1
print('fail to process ',code)
finally:
return XianJinDic
def writeJson(List,path):
res=json.dumps(List, indent=4, ensure_ascii=False)
with open(path,'w',encoding='utf-8') as f:
print("writing file ", path, ".......")
f.write(res)
def readFile(path,startline):
List=[]
with open(path,'r',encoding='utf-8') as f:
while(startline>0):
code=f.readline().replace('.SZ','').replace('.SH','').replace('.OC','').replace('\n','')
startline-=1
while(code):
List.append(code)
code=f.readline().replace('.SZ','').replace('.SH','').replace('.OC','').replace('\n','')
return List
def readJson(path):
with open(path,'r',encoding='utf-8') as f:
InfoList=json.load(f)
return InfoList
def Spider(companyCodeFile,BaseInfoPath,LiRunPath,XianJinPath,ZhiBiaoPath):
companyCodeList = readFile(companyCodeFile,0)
BaseInfoList=[]
LiRunList=[]
XianJinList=[]
ZhiBiaoList=[]
for code in companyCodeList:
writeJson(BaseInfoList.append(getBaseInfo(code)),BaseInfoPath)
for code in companyCodeList:
writeJson(LiRunList.append(getLiRun(code)),LiRunPath)
for code in companyCodeList:
writeJson(XianJinList.append(getXianJin(code)),XianJinPath)
for code in companyCodeList:
writeJson(ZhiBiaoList.append(getZhiBiao(code)),ZhiBiaoPath)
if __name__=='__main__':
#共有3999个公司代码
companyCodeFile = '证券代码.txt'
BaseInfoPath ='3999_BaseInfoList.json'
LiRunPath = '3999_LiRunList.json'
XianJinPath = '3999_XianJinList.json'
ZhiBiaoPath = '3999_ZhiBiaoList.json'
Spider(companyCodeFile,BaseInfoPath,LiRunPath,XianJinPath,ZhiBiaoPath)