-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathSinaSearchAdvanced.m
More file actions
242 lines (200 loc) · 5.89 KB
/
SinaSearchAdvanced.m
File metadata and controls
242 lines (200 loc) · 5.89 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
function [NewsDataCell] = SinaSearchAdvanced(StringIncludeAll,BeginDate,EndDate,ChannelFlag)
% by LiYang_faruto
% Email:farutoliyang@foxmail.com
% 2015/01/01
%{
http://search.sina.com.cn/?c=news
&q=600588&range=all
&time=custom&stime=2014-12-23&etime=2014-12-31
&num=20&col=1_7
%}
%% 输入输出预处理
Charset = 'gb2312';
if nargin < 4 || isempty(ChannelFlag)
% 新浪搜索频道选择 1-财经频道 2-新闻频道
ChannelFlag = 1;
end
if nargin < 3 || isempty(EndDate)
EndDate = '2014-12-26';
end
if nargin < 2 || isempty(BeginDate)
BeginDate = '2014-12-26';
end
if nargin < 1 || isempty(StringIncludeAll)
StringIncludeAll = '600588';
end
% 股票代码预处理,目标代码demo '600588'
if strcmpi(StringIncludeAll(1),'s')
StringIncludeAll = StringIncludeAll(3:end);
end
if strcmpi(StringIncludeAll(end),'h') || strcmpi(StringIncludeAll(end),'z')
StringIncludeAll = StringIncludeAll(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
NewsDataCell = [];
%% URL生成
%{
http://search.sina.com.cn/?c=news
&q=600588&range=all
&time=custom&stime=2014-12-23&etime=2014-12-31
&num=20&col=1_7
%}
% "包含以下全部的关键词"对应的输入字符,中文字符需要转换成GB2312编码
temp = Unicode2URLHexCode_Ch(StringIncludeAll);
q = temp;
% 开始日期
stime = BeginDate;
% 结束日期
etime = EndDate;
% 搜索结果显示条数(选择搜索结果显示的条数)
num = '20';
% 新浪搜索频道选择
if ChannelFlag == 1
col = '1_7';
end
if ChannelFlag == 2
col = '1_3';
end
%{
http://search.sina.com.cn/?c=news
&q=600588&range=all
&time=custom&stime=2014-12-23&etime=2014-12-31
&num=20&col=1_7
%}
URL = ['http://search.sina.com.cn/?c=news', ...
'&q=',q,'&range=all&time=custom&stime=',stime,...
'&etime=',etime,'&num=',num,'&col=',col];
%% 数据获取
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);
%% 正则处理
DataCell = SinaURLcharParse(URLchar);
%% 获取其他页码的搜索结果
DCell = [];
expr = ['<!-- 分页 begin -->','.*?', ...
'<!-- 分页 end -->'];
PageStr = regexpi(URLchar, expr,'match');
PageStr = PageStr{1};
expr = ['<a href=','.*?', ...
'</a>'];
MatchStr = regexpi(PageStr, expr,'match');
Len = numel(MatchStr);
if Len > 0
for i = 1:Len-1
temp = MatchStr{i};
expr = ['"'];
out = regexpi(temp, expr,'split');
out = out{2};
tURL = ['http://search.sina.com.cn/',out];
tURL = regexprep(tURL,'&','&');
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 = SinaURLcharParse(URLchar);
DCell = [DCell;tData(2:end,:)];
end
end
%% 输出
NewsDataCell = [DataCell;DCell];
temp1 = NewsDataCell(1,:);
temp2 = NewsDataCell(end:(-1):2,:);
NewsDataCell = [temp1;temp2];
end
% [EOF_Main]
%% Sub Function
function DataCell = SinaURLcharParse(URLchar)
% by LiYang_faruto
% Email:farutoliyang@foxmail.com
% 2015/01/01
%% 输入输出预处理
if isempty( URLchar )
DataCell = [];
return;
end
DataCell = [];
%%
% &rsv_page=2
expr = ['<!-- 文字新闻.*?begin -->','.*?',...
'<!-- 文字新闻.*?end --> '];
[matchstart,matchend,tokenindices,matchstring,tokenstring,tokenname,splitstring] = ...
regexpi(URLchar, expr);
Len = numel(matchstring);
if Len>1
ColNum = 4;
DataCell = cell(Len, ColNum);
% Head = {'DateTime','Title','Source','URL'};
for i = 1:Len
StringTemp = matchstring{i};
expr = ['<a href=','.*?',...
'</a>'];
TitleURL = regexpi(StringTemp, expr,'match');
TitleURL = TitleURL{1};
expr = ['>','.*?',...
'</a>'];
out = regexpi(TitleURL, expr,'match');
out = out{1};
temp = out(2:end-4);
% % % 简易预处理清洗,剔除<em> </em>
expr = ['<.*?em>'];
replace = '';
temp = regexprep(temp,expr,replace);
% Title
DataCell{i,2} = temp;
expr = ['"'];
out = regexpi(TitleURL, expr,'split');
out = out{2};
temp = out;
% URL
DataCell{i,4} = temp;
expr = ['<span class="fgray_time">','.*?',...
'</span>'];
AuthorDate = regexpi(StringTemp, expr,'match');
AuthorDate = AuthorDate{1};
expr = ['>','.*?',...
'</span>'];
out = regexpi(AuthorDate, expr,'match');
out = out{1};
temp = out(2:end-7);
expr = [' '];
out = regexpi(temp, expr,'split');
if numel(out) == 3
% Author Source
DataCell{i,3} = out{1};
% DateTime
DataCell{i,1} = [out{2},' ',out{3}];
else
% 有的可能没有Author i.e. 网站名称(作者名称)没有
% Author Source
DataCell{i,3} = [];
% DateTime
DataCell{i,1} = [out{1},' ',out{2}];
end
end
Head = {'DateTime','Title','Source','URL'};
DataCell = [Head;DataCell];
end
end