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
| import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.lang.Integer;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class GETcnki {
public static void readContentFromGet(String GET_URL) {
String lines;
URL cnkiURL = null;
try {
cnkiURL = new URL(GET_URL);
} catch (java.net.MalformedURLException e) {
e.printStackTrace();
readContentFromGet(GET_URL);
}
List<String> cookies = null;
StringBuffer response = new StringBuffer();
// 打开连接,URL.openConnection函数会根据URL的类型,
// 返回不同的URLConnection子类的对象,URL是http,返回HttpURLConnection
try {
HttpURLConnection connection = (HttpURLConnection) cnkiURL.openConnection();
connection.setRequestProperty("User-Agent",
"Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:29.0) Gecko/20100101 Firefox/29.0");
connection.connect();
/*
String headerName = null;
for (int i = 1; (headerName = connection.getHeaderFieldKey(i)) != null;
i++) {
System.out.println(headerName + ":" + connection.getHeaderField(i));
}
*/
cookies = connection.getHeaderFields().get("Set-Cookie");
BufferedReader reader = new BufferedReader(new InputStreamReader(
connection.getInputStream(), "utf-8"));
while ((lines = reader.readLine()) != null) {
response.append(lines);
}
reader.close();
connection.disconnect();
//System.out.println("----------\n"+response);
} catch (java.io.IOException e) {
e.printStackTrace();
readContentFromGet(GET_URL);
}
//---------------------------
URL cnkiURL2 = null;
try {
cnkiURL2 = new URL(
"http://epub.cnki.net/KNS/brief/brief.aspx?pagename=" +
response.toString());
} catch (java.net.MalformedURLException e) {
e.printStackTrace();
readContentFromGet(GET_URL);
}
StringBuffer response2 = new StringBuffer();
try {
HttpURLConnection con = (HttpURLConnection) cnkiURL2.openConnection();
con.setRequestProperty("User-Agent",
"Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:29.0) Gecko/20100101 Firefox/29.0");
con.setRequestProperty("Referer",
"http://epub.cnki.net/KNS/brief/result.aspx?dbprefix=CDMD");
String newcookies = "";
for (String cookie : cookies) {
newcookies += (cookie.substring(0, cookie.indexOf(";")) + ";");
}
//System.out.println("newcookies:" + newcookies);
//ASP.NET_SessionId=oaxxoh3bazgbhcuei5aharzk; LID=; SID_kns=120113
con.addRequestProperty("Cookie", newcookies);
/*
for (String header : con.getRequestProperties().keySet()) {
if (header != null) {
for (String value : con.getRequestProperties().get(header)) {
System.out.println(header + ":" + value);
}
}
}
*/
con.connect();
BufferedReader reader2 = new BufferedReader(new InputStreamReader(
con.getInputStream(), "utf-8"));
while ((lines = reader2.readLine()) != null) {
response2.append(lines);
}
reader2.close();
con.disconnect();
} catch (java.io.IOException e) {
e.printStackTrace();
readContentFromGet(GET_URL);
}
String info = response2.toString();
//句子 1([\s\S]+?)句子来
List<String> sentences = GETcnki.StrMatch(info,
"句子 1([\\s\\S]+?)句子来");
for(int i =0;i<sentences.size();i++){
//去除标签
sentences.set(i,sentences.get(i).replaceAll("<[^>]*>",""));
//分段
Matcher mat =
Pattern.compile("句子 ").matcher(sentences.get(i));
while (mat.find())
sentences.set(i,
sentences.get(i).replaceAll("句子 ","\n句子"));
sentences.set(i,"句子1"+sentences.get(i));
}
int i = 0;
//har\('(.+?)'
List<String> titles = GETcnki.StrMatch(info, "har\\('(.+?)'");
//作者.+?">(.+?)<
List<String> auhtors = GETcnki.StrMatch(info, "作者.+?\">(.+?)<");
//文献来源.+?">(.+?)<
List<String> literatures = GETcnki.StrMatch(info, "文献来源.+?\">(.+?)<");
//en.{2}发表时间.+?>(.+?\b)<
List<String> dates = GETcnki.StrMatch(info, "en.{2}发表时间.+?>(.+?\\b)<");
//下载频次.+?>([\d]*)
List<String> downloadfrequencys =
GETcnki.StrMatch(info,"下载频次.+?>([\\d]*)");
//来源库.+?>(.+?)[\s]+?<
List<String> databases =
GETcnki.StrMatch(info,"来源库.+?>(.+?)[\\s]+?<");
for(i =0;i<sentences.size();i++){
System.out.println(i + ":" + titles.get(i)
+" "+auhtors.get(i)+" "+databases.get(i)
+"\n"+literatures.get(i)+" "+dates.get(i)
+" 下载频次:"+downloadfrequencys.get(i)+"\n"+sentences.get(i)+"\n");
}
}
public static List<String> StrMatch(String raw, String regex) {
List<String> result = new ArrayList<String>();
Pattern pat = Pattern.compile(regex);
Matcher mat = pat.matcher(raw);
if (mat.find()) {
//System.out.println("---\n"+mat.group(1)+"---\n");
result.add(mat.group(1));
while (mat.find())
//System.out.println("---\n"+mat.group(1)+"---\n");
//捕获组是从 1 开始从左到右的索引
result.add(mat.group(1));
} else {
return result;
}
return result;
}
public static String encodeURIComponent(String component) {
String result = null;
try {
result = URLEncoder.encode(component, "UTF-8")
.replaceAll("\\%28", "(").replaceAll("\\%29", ")")
.replaceAll("\\+", "%20").replaceAll("\\%27", "'")
.replaceAll("\\%21", "!").replaceAll("\\%7E", "~");
} catch (UnsupportedEncodingException e) {
result = component;
}
return result;
}
public static void main(String[] args) {
String[] s={"","","",""};
System.out.print("正在通过CNKI查找内容相似的论文。。。\n关键词:");
if(args.length != 4){
System.err.println("请输入四个字符串!");
return ;
}
for(int i =0 ;i<args.length;i++){
if(args[i].length() == 0 ){
System.err.println("\n请不要输入空字符串!");
return ;
}
s[i] = encodeURIComponent(args[i]);
System.out.print(args[i]+" ");
}
System.out.println("\n");
String GET_URL = "http://epub.cnki.net/KNS/request/SearchHandler.ashx?" +
"action=&NaviCode=*&ua=1.21&PageName=ASP.brief_result_aspx&DbPrefix=CDMD" +
"&DbCatalog=中国优秀博硕士学位论文全文数据库" + "&ConfigFile=CDMD.xml" +
"&db_opt=中国优秀博硕士学位论文全文数据库" +
"&db_value=中国博士学位论文全文数据库,中国优秀硕士学位论文全文数据库" +
"&sen_1_sel=%2FNEAR%2020&sen_1_value1=" + s[0] + "&sen_1_value2=" +
s[1] + "&sen_2_sel=%2FSEN%2020&sen_2_value1=" + s[2] +
"&sen_2_value2=" + s[3] + "&sen_2_logical=and" +
"&his=0&issen=1&__=Mon%20Jun%2009%202014%2022%3A15%3A59%20GMT%2B0800%20(CST)";
/*ConfigFile CDMD.xml
DbCatalog 中国优秀博硕士学位论文全文数据库
DbPrefix CDMD
NaviCode *
PageName ASP.brief_result_aspx
__ Tue Jun 10 2014 12:08:21 GMT+0800 (CST)
action
db_opt 中国优秀博硕士学位论文全文数据库
db_value 中国博士学位论文全文数据库,中国优秀硕士学位论文全文数据库
his 0
issen 1
sen_1_sel /NEAR 20
sen_1_value1 信息
sen_1_value2 某种程度
sen_2_logical and
sen_2_sel /SEN 20
sen_2_value1 证券市场
sen_2_value2 最具敏感性
ua 1.21
*/
GETcnki gs = new GETcnki();
gs.readContentFromGet(GET_URL);
}
}
|