forked from CharlesZhong/CnkiProject
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CnkiSpider.py
348 lines (300 loc) · 10.5 KB
/
CnkiSpider.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
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
#!/usr/bin/python
# -*- coding: utf-8 -*-
# CnkiSpider.py crawl data from '*.cnki.com' And Store data into files
# author : zhongxin1990@gmail.com
import urllib2
from urllib2 import Request, urlopen, URLError, HTTPError
from urllib import urlencode
from bs4 import BeautifulSoup
import re
import MySQLdb
from time import time
import os
class cnkiSpider():
def __init__(self):
self.author = ''
self.department = ''
self.data = ''
self.headers = {
#'Accept':'*/*',
#'Accept-Encoding':'gzip,deflate,sdch',
#'Accept-Language':'zh-CN,zh;q=0.8,en;q=0.6,zh-TW;q=0.4',
'Connection':'keep-alive',
'Cookie':'ASP.NET_SessionId=pfcjth3z04fl3uiydy5v3c45; LID=; LID=WEEvREcwSlJHSldTTGJhYlN2SDBwYy82emtTN0hmcVNmNW5qSDdxNjlGc096MzdTeDZFRFMybXhBVmJQRytKMQ==; SID_kns=120102; RsPerPage=50; cnkiUserKey=e4fd0059-f300-cb77-bcae-c122cf71ac25; CurTop10KeyWord=%2c%u5b59%u5bb6%u5e7f%2c%u6e05%u534e%u5927%u5b66',
'Host':'epub.cnki.net',
'Referer':'http://epub.cnki.net/KNS/brief/result.aspx?dbprefix=scdb&action=scdbsearch&db_opt=SCDB',
'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.57 Safari/537.36'
}
self.keywordDir = "keyword/"
self.paperDir = "paper/"
self.index = 0
#self.num = 0
def session_con(self):
try:
baseurl = 'http://epub.cnki.net/KNS/request/SearchHandler.ashx?'
url = baseurl + self.generate_Url(self.author,self.department,action='')
req = urllib2.Request(url,self.data,self.headers)
response = urllib2.urlopen(req)
#sessionResult = response.read()
except urllib2.URLError, e:
print e
except HTTPError, e:
print 'The server couldn\'t fulfill the request.'
print 'Error code: ', e.code
except Exception:
print Exception
#return sessionResult
def keyword_con(self):
try:
baseurl = 'http://epub.cnki.net/KNS/group/DoGroupLeft.ashx?'
url = baseurl + self.generate_Url(self.author,self.department,action='44')
req = urllib2.Request(url,self.data,self.headers)
response = urllib2.urlopen(req)
keywordResult = response.read()
self.storeData(self.id,keywordResult,self.keywordDir)
except urllib2.URLError, e:
print e
except HTTPError, e:
print 'The server couldn\'t fulfill the request.'
print 'Error code: ', e.code
except Exception:
print Exception
#return keywordResult
def paper_con(self):
try:
#baseurl = 'http://epub.cnki.net/KNS/group/DoGroupLeft.ashx?'
url = 'http://epub.cnki.net/KNS/brief/brief.aspx?pagename=ASP.brief_result_aspx&dbPrefix=SCDB&dbCatalog=%e4%b8%ad%e5%9b%bd%e5%ad%a6%e6%9c%af%e6%96%87%e7%8c%ae%e7%bd%91%e7%bb%9c%e5%87%ba%e7%89%88%e6%80%bb%e5%ba%93&ConfigFile=SCDB.xml&research=off&t=1385383351937&keyValue=&S=1'
req = urllib2.Request(url,self.data,self.headers)
response = urllib2.urlopen(req)
PaperResult = response.read()
total = self.parsePaperTotalNum(PaperResult)
if total > 50:
self.headers['Cookie'] = 'ASP.NET_SessionId=pfcjth3z04fl3uiydy5v3c45; LID=; LID=WEEvREcwSlJHSldTTGJhYlN2SDBwYy82emtTN0hmcVNmNW5qSDdxNjlGc096MzdTeDZFRFMybXhBVmJQRytKMQ==; SID_kns=120102; RsPerPage='+total+'; cnkiUserKey=e4fd0059-f300-cb77-bcae-c122cf71ac25; CurTop10KeyWord=%2c%u5b59%u5bb6%u5e7f%2c%u6e05%u534e%u5927%u5b66'
req = urllib2.Request(url,self.data,self.headers)
response = urllib2.urlopen(req)
PaperResult = response.read()
self.storeData(self.id,PaperResult,self.paperDir)
except urllib2.URLError, e:
print e.reason
except HTTPError, e:
print 'The server couldn\'t fulfill the request.'
print 'Error code: ', e.code
except Exception:
print Exception
#return PaperResult
def generate_Url(self,author,department,action):
if not isinstance(department,list) or len(department) == 1:
param = {
'action':action,
'NaviCode':'*',
'ua':'1.21',
'PageName':'ASP.brief_result_aspx',
'DbPrefix':'SCDB',
'DbCatalog':'中国学术文献网络出版总库',
'ConfigFile':'SCDB.xml',
'db_opt':'CJFQ,CJFN,CDFD,CMFD,CPFD,IPFD,CCND,CCJD,HBRD',
'base_special1':'%',
'magazine_special1':'%',
'au_1_sel':'AU',
'au_1_sel2':'AF',
'au_1_value1':author,
'au_1_value2':department,
'au_1_special1':'=',
'au_1_special2':'%',
'his':'0',
'__':'Mon Nov 25 2013 19:39:40 GMT+0800 (CST)',
}
elif len(department) == 2:
param = {
'action':action,
'NaviCode':'*',
'ua':'1.21',
'PageName':'ASP.brief_result_aspx',
'DbPrefix':'SCDB',
'DbCatalog':'中国学术文献网络出版总库',
'ConfigFile':'SCDB.xml',
'db_opt':'CJFQ,CJFN,CDFD,CMFD,CPFD,IPFD,CCND,CCJD,HBRD',
'base_special1':'%',
'magazine_special1':'%',
'au_1_sel':'AU',
'au_1_sel2':'AF',
'au_1_value1':author,
'au_1_value2':department[0],
'au_1_special1':'=',
'au_1_special2':'%',
'au_2_sel':'AU',
'au_2_sel2':'AF',
'au_2_value1':author,
'au_2_value2':department[1],
'au_2_logical':'or',
'au_2_special1':'=',
'au_2_special2':'%',
'his':'0',
'__':'Mon Nov 25 2013 19:39:40 GMT+0800 (CST)',
}
else:
param = {
'action':action,
'NaviCode':'*',
'ua':'1.21',
'PageName':'ASP.brief_result_aspx',
'DbPrefix':'SCDB',
'DbCatalog':'中国学术文献网络出版总库',
'ConfigFile':'SCDB.xml',
'db_opt':'CJFQ,CJFN,CDFD,CMFD,CPFD,IPFD,CCND,CCJD,HBRD',
'base_special1':'%',
'magazine_special1':'%',
'au_1_sel':'AU',
'au_1_sel2':'AF',
'au_1_value1':author,
'au_1_value2':department[0],
'au_1_special1':'=',
'au_1_special2':'%',
'au_2_sel':'AU',
'au_2_sel2':'AF',
'au_2_value1':author,
'au_2_value2':department[1],
'au_2_logical':'or',
'au_2_special1':'=',
'au_2_special2':'%',
'au_3_sel':'AU',
'au_3_sel2':'AF',
'au_3_value1':author,
'au_3_value2':department[2],
'au_3_logical':'or',
'au_3_special1':'=',
'au_3_special2':'%',
'his':'0',
'__':'Mon Nov 25 2013 19:39:40 GMT+0800 (CST)',
}
url = urlencode(param)
#print 80*'*'
#print url
return url
# the total number of papers for An export
def parsePaperTotalNum(self,paper_doc):
soup = BeautifulSoup(paper_doc)
result = soup.find(class_='pagerTitleCell')
total = filter(lambda x:x.isdigit(),str(result))
return total
# set params of an export
def setQueryParams(self,id,author,department):
self.id,self.author,self.department = id,author,self.translator(department)
def storeData(self,id,doc,dir):
f = open(os.path.join(dir,str(self.index))+'/'+str(id)+'_'+self.author+'_'+"".join(self.department)+'.html','w')
#print os.path.join(dir,str(self.index))+'/'+str(id)+'_'+self.author+'_'+"".join(self.department)+'.html'
f.write(doc)
f.close()
if dir == self.keywordDir:
print "keyword"+str(id)+"Success!",
else :
print "paper"+str(id)+"Success!"
def mkSubDir(self,index):
subKeywordDir = os.path.join(self.keywordDir,str(index))
if not os.path.isdir(subKeywordDir):
os.makedirs(subKeywordDir)
subPaperDir = os.path.join(self.paperDir,str(index))
if not os.path.isdir(subPaperDir):
os.makedirs(subPaperDir)
self.index = index
def translator(self,department):
if department.find('-') != -1 :
return department.split('-')[0]
elif department.find('|') != -1:
return department.split('|')
elif department.find('、') != -1:
return department.split('、')
else:
return department
#intab = '-\/*'
#outtab = '------'
#trantab = maketrans(intab,outtab)
return department.translate(None,'-')
class DB():
def __init__(self):
self.conn = ""
self.cursor = ""
def connect(self):
self.conn = MySQLdb.connect(host="localhost",user="root",passwd="123456",db="experts")
self.cursor = self.conn.cursor()
def close(self):
self.cursor.close()
self.conn.close()
def find(self,begin,num):
sql = "select SQL_CALC_FOUND_ROWS ZJ_NO,ZJ_NAME,DW_GUID from newExpert order by ZJ_NO Limit "+str(begin)+","+str(num)
n = self.cursor.execute(sql)
#print n
#print self.cursor.fetchall()
return self.cursor.fetchall()
def find_remain(self,begin,num):
sql = "select SQL_CALC_FOUND_ROWS ZJ_NO,ZJ_NAME,DW_GUID from newExpert order by ZJ_NO Limit "+str(begin)+","+str(num)
n = self.cursor.execute(sql)
def find_test(self,begin,num):
sql = "select distinct DW_GUID from newExpert Limit "+str(begin)+","+str(num)
n = self.cursor.execute(sql)
#print n
#print self.cursor.fetchall()
return self.cursor.fetchall()
def main(start):
scale = 1000
spider = cnkiSpider()
db = DB()
for index in range((start/scale)+1,20):
n = index*scale
spider.mkSubDir(index)
db.connect()
expertSet = db.find(index*scale,scale)
db.close()
start = time()
a = time()
for AnExpert in expertSet:
print "n: ",n,
n = n+1
spider.setQueryParams(AnExpert[0],AnExpert[1],AnExpert[2])
spider.session_con()
spider.keyword_con()
spider.paper_con()
if n % 100 == 0:
b = time()
f = open("log.txt","a")
f.write("total:"+str(n)+"\t"+str(b-a)+"\n")
f.close()
a = b
end = time()
print "time:",(end-start)
def main_remain(remainNum):
#892
#1785
scale = 1000
#remainNum = 892
subDir = remainNum / scale
spider = cnkiSpider()
spider.mkSubDir(subDir)
db = DB()
db.connect()
expertSet = db.find(remainNum,scale-(remainNum%scale))
#expertSet = db.find(1*scale,scale)
db.close()
n = remainNum
start = time()
a = time()
for AnExpert in expertSet:
print "n: ",n,
n = n+1
spider.setQueryParams(AnExpert[0],AnExpert[1],AnExpert[2])
spider.session_con()
spider.keyword_con()
spider.paper_con()
end = time()
print "time:",(end-start)
'''
for AnExpert in expertSet:
for row in AnExpert:
print row,
print
print "total:",len(expertSet)
print spider.index
'''
if __name__ =="__main__":
main_remain(17610)
#main(17542)