-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathGetStockNotice_Web.m
More file actions
375 lines (320 loc) · 9.61 KB
/
GetStockNotice_Web.m
File metadata and controls
375 lines (320 loc) · 9.61 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
function [NoticeDataCell] = GetStockNotice_Web(StockCode,BeginDate,EndDate)
% by LiYang_faruto
% Email:farutoliyang@foxmail.com
% 2015/01/01
%{
http://www.cninfo.com.cn/search/stockfulltext.jsp?
orderby=date11
¬iceType=0107&keyword=
&startTime=2005-01-01&endTime=2014-12-31
&stockCode=600588&pageNo=1
%}
%% 输入输出预处理
Charset = 'gb2312';
if nargin < 3 || isempty(EndDate)
EndDate = '2014-06-01';
end
if nargin < 2 || isempty(BeginDate)
BeginDate = '2015-01-01';
end
if nargin < 1 || isempty(StockCode)
StockCode = '600588';
end
% 股票代码预处理,目标代码demo '600588'
if strcmpi(StockCode(1),'s')
StockCode = StockCode(3:end);
end
if strcmpi(StockCode(end),'h') || strcmpi(StockCode(end),'z')
StockCode = StockCode(1:end-2);
end
% 日期预处理,目标形式2014-12-29
ind = find( BeginDate == '-',1 );
if isempty(ind)
BeginDate = [BeginDate(1:4),'-',BeginDate(5:6),'-',BeginDate(7:end)];
end
ind = find( EndDate == '-',1 );
if isempty(ind)
EndDate = [EndDate(1:4),'-',EndDate(5:6),'-',EndDate(7:end)];
end
NoticeDataCell = [];
%% NoticeTypeCell
NoticeTypeCell = {'010301','年度报告'; ...
'010303','半年度报告'; ...
'010305','一季度报告'; ...
'010307','三季度报告'; ...
'0102','首次公开发行及上市'; ...
'0105','配股'; ...
'0107','增发'; ...
'0109','可转换债券'; ...
'0110','权证相关公告'; ...
'0111','其它融资'; ...
'0113','权益及限制出售股份'; ...
'0115','股权变动'; ...
'0117','交易'; ...
'0119','股东大会'; ...
'0121','澄清、风险、业绩预告'; ...
'0125','特别处理和退市'; ...
'0127','补充及更正'; ...
'0129','中介机构报告'; ...
'0131','上市公司制度'; ...
'0123','其它重大事项'; ...
};
%% NoticeDataCell
Head = {'StockCode','DateTime','Title','NoticeType','FileURL','FileSize'};
NoticeDataCell = [Head;NoticeDataCell];
Rnum = size(NoticeTypeCell,1);
% 1:Rnum
for i = 1:Rnum
str = ['========='];
disp(str);
i
NoticeTypeCell{i,2}
str = ['============'];
disp(str);
NoticeType = NoticeTypeCell{i,1};
% NoticeType = [];
tCell = subGetStockNotice_Web(StockCode,BeginDate,EndDate,NoticeType);
NoticeDataCell = [NoticeDataCell;tCell(2:end,:)];
end
if size(NoticeDataCell,1) == 1
NoticeDataCell = [];
return;
end
%% NoticeDataCell按照时间进行排序
tH = NoticeDataCell(1,:);
tD = NoticeDataCell(2:end,:);
Temp = cellfun( @Dstr2Dnum,tD(:,2) );
[~,I] = sort(Temp);
temp = tD(I,:);
NoticeDataCell = [tH;temp];
end
% % % [EOF_GetStockNotice_Web]
%% sub func-Dstr2Dnum
function y = Dstr2Dnum(Dstr)
ind = find(Dstr == ':',1);
if isempty(ind)
y = datenum( Dstr,'yyyy-mm-dd' );
else
y = datenum( Dstr,'yyyy-mm-dd HH:MM' );
end
end
% % % [EOF_Dstr2Dnum]
%% sub function-subGetStockNotice_Web
function tCell = subGetStockNotice_Web(StockCode,BeginDate,EndDate,NoticeType)
%% 初始化
tCell = [];
if strcmpi(NoticeType,'All')
NoticeType = [];
end
Head = {'StockCode','DateTime','Title','NoticeType','FileURL','FileSize'};
NoticeTypeCell = {'010301','年度报告'; ...
'010303','半年度报告'; ...
'010305','一季度报告'; ...
'010307','三季度报告'; ...
'0102','首次公开发行及上市'; ...
'0105','配股'; ...
'0107','增发'; ...
'0109','可转换债券'; ...
'0110','权证相关公告'; ...
'0111','其它融资'; ...
'0113','权益及限制出售股份'; ...
'0115','股权变动'; ...
'0117','交易'; ...
'0119','股东大会'; ...
'0121','澄清、风险、业绩预告'; ...
'0125','特别处理和退市'; ...
'0127','补充及更正'; ...
'0129','中介机构报告'; ...
'0131','上市公司制度'; ...
'0123','其它重大事项'; ...
};
if ~isempty(NoticeType)
Temp = cellfun(@(x)strcmpi(x,NoticeType),NoticeTypeCell(:,1));
NoticeTypeWord = NoticeTypeCell{Temp,2};
else
NoticeTypeWord = [];
end
%% URL生成
%{
http://www.cninfo.com.cn/search/stockfulltext.jsp?
orderby=date11
¬iceType=0107&keyword=
&startTime=2005-01-01&endTime=2014-12-31
&stockCode=600588&pageNo=1
%}
URL = ['http://www.cninfo.com.cn/search/stockfulltext.jsp?orderby=date11', ...
'¬iceType=',NoticeType,'&keyword=',...
'&startTime=',BeginDate,'&endTime=',EndDate,'&stockCode=',StockCode, ...
'&pageNo=1'];
%% 数据获取
Charset = 'gb2312';
if verLessThan('matlab', '8.3')
[URLchar, status] = urlread_General(URL, 'Charset', Charset, 'TimeOut', 60);
else
[URLchar, status] = urlread(URL, 'Charset', Charset, 'TimeOut', 60);
end
if status == 0
str = ['urlread error:网页读取失败!请检查输入的网址或网络连接情况!'];
disp(str);
return;
end
% URLString = java.lang.String(URLchar);
expr = ['<span class="count"','.*?', ...
'</span>'];
Temp = regexpi(URLchar, expr,'match');
Temp = Temp{1};
expr = ['共','.*?', ...
'条'];
Temp = regexpi(URLchar, expr,'match');
Temp = Temp{1};
Temp = Temp(2:end-1);
Temp = str2num(Temp);
if Temp == 0
return;
end
%% 正则处理
DataCell = subURLcharParse(URLchar,StockCode,NoticeTypeWord);
%% 获取其他页码的搜索结果
DCell = [];
expr = ['onclick=','''','goPage(','.*?', ...
'<'];
PageStr = regexpi(URLchar, expr,'match');
if ~isempty(PageStr)
PageStr = PageStr(end);
PageStr = PageStr{1};
expr = ['>','.*?', ...
'<'];
MatchStr = regexpi(PageStr, expr,'match');
MatchStr = MatchStr{1};
MatchStr = regexprep(MatchStr,'>','');
MatchStr = regexprep(MatchStr,'<','');
Len = str2double(MatchStr);
else
Len = 0;
end
if Len > 1
% 2:Len
for i = 2:Len
PageNumDebug = i
tURL = ['http://www.cninfo.com.cn/search/stockfulltext.jsp?orderby=date11', ...
'¬iceType=',NoticeType,'&keyword=',...
'&startTime=',BeginDate,'&endTime=',EndDate,'&stockCode=',StockCode, ...
'&pageNo=',num2str(i)];
if verLessThan('matlab', '8.3')
[URLchar, status] = urlread_General(tURL, 'Charset', Charset, 'TimeOut', 60);
else
[URLchar, status] = urlread(tURL, 'Charset', Charset, 'TimeOut', 60);
end
if status == 0
str = ['urlread error:网页读取失败!请检查输入的网址或网络连接情况!'];
disp(str);
URLchar = [];
end
tData = subURLcharParse(URLchar,StockCode,NoticeTypeWord);
DCell = [DCell;tData(2:end,:)];
end
end
%% 输出
tCell = [DataCell;DCell];
if isempty(tCell)
return;
end
temp1 = tCell(1,:);
temp2 = tCell(end:(-1):2,:);
tCell = [temp1;temp2];
end
% % % [EOF_subGetStockNotice_Web]
%% Sub Function
function DataCell = subURLcharParse(URLchar,StockCode,NoticeTypeWord)
% by LiYang_faruto
% Email:farutoliyang@foxmail.com
% 2015/01/01
%% 输入输出预处理
if isempty( URLchar )
DataCell = [];
return;
end
DataCell = [];
Head = {'StockCode','DateTime','Title','NoticeType','FileURL','FileSize'};
%%
% &rsv_page=2
expr = ['<td class="qsgg">','.*?',...
'</tr>'];
[matchstart,matchend,tokenindices,matchstring,tokenstring,tokenname,splitstring] = ...
regexpi(URLchar, expr);
Len = numel(matchstring);
if Len>0
ColNum = length(Head);
DataCell = cell(Len, ColNum);
% Head = {'DateTime','StockCode','Title','NoticeType','FileURL','FileSize'};
for i = 1:Len
DebugItermNum = i
DataCell{i,1} = StockCode;
DataCell{i,4} = NoticeTypeWord;
StringTemp = matchstring{i};
expr = ['<a href=','.*?',...
'</a>'];
TitleURL = regexpi(StringTemp, expr,'match');
if ~isempty(TitleURL)
TitleURL = TitleURL{1};
expr = ['>','.*?',...
'</a>'];
out = regexpi(TitleURL, expr,'match');
out = out{1};
temp = out(2:end-length('</a>'));
% % % 简易预处理清洗,剔除<em> </em>
expr = ['<.*?em>'];
replace = '';
temp = regexprep(temp,expr,replace);
% Title
DataCell{i,3} = temp;
expr = ['"'];
out = regexpi(TitleURL, expr,'split');
out = out{2};
temp = out;
% URL
DataCell{i,5} = ['http://www.cninfo.com.cn/',temp];
else
% Title
DataCell{i,3} = [];
% URL
DataCell{i,5} = [];
end
% % FileSize
expr = ['<img','.*?',...
'</td>'];
out = regexpi(StringTemp, expr,'match');
if ~isempty(out)
out = out{1};
expr = ['width=16> (','.*?',...
') </td>'];
out = regexpi(out, expr,'match');
out = out{1};
ind = (1+length('width=16> (')):(length(out)-length(') </td>'));
temp = out(ind);
else
temp = [];
end
DataCell{i,6} = temp;
% % DateTime
expr = ['<td class="ggsj"','.*?',...
'</td>'];
DateTime = regexpi(StringTemp, expr,'match');
if ~isempty(DateTime)
DateTime = DateTime{1};
expr = ['>','.*?',...
'</td>'];
out = regexpi(DateTime, expr,'match');
out = out{1};
expr = [' '];
out = regexprep(out, expr,'');
temp = out(2:end-length('</td>'));
else
temp = [];
end
DataCell{i,2} = temp;
end
DataCell = [Head;DataCell];
end
end
% % % [EOF_subURLcharParse]