This repository was archived by the owner on Apr 22, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspider.py
404 lines (370 loc) · 17.4 KB
/
spider.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
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
import json
import execjs
import re
from urllib import parse
from datetime import datetime, timedelta
import logging
from parameter import Parameter
from session import Session
from condition import Condition
from error import CheckCodeError, NullContentError
MAX_PAGE = 10
class Spider:
def __init__(self, sess: Session):
self.logger = logging.getLogger('spider')
self.sess = sess
with open('./js/docid.js') as f:
js = f.read()
self.js_docid = execjs.compile(js)
def content_list(self, param: Parameter, page, order, direction, index=1, total=0) -> (dict, int):
"""
获取内容列表
page: 每页几条
order: 排序标准
direction: 顺序 (asc - 正序 desc - 倒序)
"""
count = 0
json_error_retry_time = 5
while True:
self.logger.info('第 {0} 页'.format(index))
# 获取数据
url = "http://wenshu.court.gov.cn/List/ListContent"
headers = {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Accept-Language": "zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7",
"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
"Host": "wenshu.court.gov.cn",
"Origin": "http://wenshu.court.gov.cn",
"Connection": "keep-alive",
"DNT": "1",
"Referer": "http://wenshu.court.gov.cn/list/list/?sorttype=1&number={0}&guid={1}&conditions=searchWord+QWJS+++{2}".format(
param.number, param.guid, parse.quote(param.param)),
"User-Agent": self.sess.user_agent,
"X-Requested-With": "XMLHttpRequest"
}
data = {
"Param": param.param,
"Index": index,
"Page": page,
"Order": order,
"Direction": direction,
"vl5x": param.vl5x,
"number": param.number[0:4],
"guid": param.guid
}
req = self.sess.post(url=url, headers=headers, data=data)
req.encoding = 'utf-8'
return_data = req.text.replace('\\', '').replace('"[', '[').replace(']"', ']') \
.replace('“', '“').replace('”', '”')
if return_data == '"remind"' or return_data == '"remind key"':
raise CheckCodeError('CheckCode Appeared in content_list')
# CheckCode(sess=self.sess)
else:
try:
json_data = json.loads(return_data)
json_error_retry_time = 5
except Exception as e:
self.logger.error('JSON Error: {}.'.format(str(e)))
# If there are 5 JSON errors, skip this page
json_error_retry_time -= 1
if json_error_retry_time == 0:
self.logger.critical('Skip the page {} for so many json errors.'.format(index))
index += 1
if index > MAX_PAGE or (total != 0 and count >= total):
break
continue
if not len(json_data):
self.logger.info('Finished.')
break
else:
run_eval = json_data[0]['RunEval']
total = int(json_data[0]['Count'])
count = count + len(json_data) - 1
for i in range(1, len(json_data)):
case_name = json_data[i]['案件名称'] if '案件名称' in json_data[i] else ''
court_name = json_data[i]['法院名称'] if '法院名称' in json_data[i] else ''
case_number = json_data[i]['案号'] if '案号' in json_data[i] else ''
case_type = json_data[i]['案件类型'] if '案件类型' in json_data[i] else ''
trial_proc = json_data[i]['审判程序'] if '审判程序' in json_data[i] else ''
doc_id = json_data[i]['文书ID'] if '文书ID' in json_data[i] else ''
doc_id = self._decrypt_id(run_eval, doc_id)
date = json_data[i]['裁判日期'] if '裁判日期' in json_data[i] else ''
full_text = json_data[i]['裁判要旨段原文'] if '裁判要旨段原文' in json_data[i] else ''
data_dict = dict(
id=doc_id,
number=case_number,
name=case_name,
type=case_type,
date=date,
court=court_name,
proc=trial_proc,
text=full_text
)
yield data_dict, index
index += 1
param.refresh()
if index > MAX_PAGE or (total != 0 and count >= total):
break
def _decrypt_id(self, run_eval, doc_id):
"""
解密DocID
"""
if len(doc_id) > 0:
js = self.js_docid.call("GetJs", run_eval)
js_objs = js.split(";;")
js1 = js_objs[0] + ';'
js2 = re.findall(r"_\[_\]\[_\]\((.*?)\)\(\);", js_objs[1])[0]
key = self.js_docid.call("EvalKey", js1, js2)
key = re.findall(r"\"([0-9a-z]{32})\"", key)[0]
return self.js_docid.call("DecryptDocID", key, doc_id)
else:
return doc_id
def tree_content(self, param: Parameter):
"""
获取左侧类目分类
"""
url = 'http://wenshu.court.gov.cn/List/TreeContent'
headers = {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Accept-Language": "zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7",
"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
"Host": "wenshu.court.gov.cn",
"Origin": "http://wenshu.court.gov.cn",
"Proxy-Connection": "keep-alive",
"Referer": "http://wenshu.court.gov.cn/list/list/?sorttype=1&number={0}&guid={1}&conditions=searchWord+QWJS+++{2}".format(
param.number, param.guid, parse.quote(param.param)),
"User-Agent": self.sess.user_agent,
"X-Requested-With": "XMLHttpRequest"
}
data = {
"Param": param.param,
"vl5x": param.vl5x,
"number": param.number,
"guid": param.guid
}
while True:
r = self.sess.post(url=url, headers=headers, data=data)
t = r.text.replace('\\', '').replace('"[', '[').replace(']"', ']')
if len(t) <= 0:
raise NullContentError('Receive null content in tree_content.')
elif t == '"remind"' or t == '"remind key"':
# self.logger.warning('出现验证码', end='\r')
raise CheckCodeError('CheckCode Appeared in tree_content.')
# CheckCode(sess=self.sess)
else:
break
json_data = json.loads(t)
tree_dict = {}
for type_data in json_data:
type_name = type_data['Key']
type_dict = {
'IntValue': type_data['IntValue'],
'ParamList': []
}
for data in type_data['Child']:
if data['IntValue']:
type_dict['ParamList'].append({'Key': data['Key'], 'IntValue': data['IntValue']})
tree_dict[type_name] = type_dict
return tree_dict
def court_tree_content(self, condition: Condition, parval: str):
"""
获取每个省市的法院列表
:return: A list of court names
"""
url = 'http://wenshu.court.gov.cn/List/CourtTreeContent'
headers = {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Accept-Language": "zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7",
"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
"Host": "wenshu.court.gov.cn",
"Origin": "http://wenshu.court.gov.cn",
"Proxy-Connection": "keep-alive",
"Referer": "http://wenshu.court.gov.cn/list/list/?sorttype=1&&conditions=searchWord+2+AJLX++{0}".format(
parse.quote(str(condition))),
"User-Agent": self.sess.user_agent,
"X-Requested-With": "XMLHttpRequest"
}
data = {
"Param": str(condition),
"parval": parval
}
while True:
r = self.sess.post(url=url, headers=headers, data=data)
t = r.text.replace('\\', '').replace('"[', '[').replace(']"', ']')
if len(t) <= 0:
raise NullContentError('Receive null content in court_tree_content.')
elif t == '"remind"' or t == '"remind key"':
raise CheckCodeError('CheckCode Appeared in court_tree_content.')
else:
break
json_data = json.loads(t)
tree_dict = {}
for type_data in json_data:
type_name = type_data['Key']
type_dict = {
'IntValue': type_data['IntValue'],
'ParamList': []
}
for data in type_data['Child']:
if data['IntValue']:
type_dict['ParamList'].append({'Key': data['Key'], 'IntValue': data['IntValue']})
tree_dict[type_name] = type_dict
if '法院层级' in tree_dict:
self.logger.critical('Error in court tree content for condition: {}'.format(str(condition)))
return {'中级法院': {'ParamList': []}, '基层法院': {'ParamList': []}}
return tree_dict
def reason_tree_content(self, condition: Condition, parval: str):
"""
获取父案由下所有的子案由
:return: A list of reasons and corresponding level
"""
url = 'http://wenshu.court.gov.cn/List/ReasonTreeContent'
headers = {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Accept-Language": "zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7",
"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
"Host": "wenshu.court.gov.cn",
"Origin": "http://wenshu.court.gov.cn",
"Proxy-Connection": "keep-alive",
"Referer": "http://wenshu.court.gov.cn/list/list/?sorttype=1&&conditions=searchWord+2+AJLX++{0}".format(
parse.quote(str(condition))),
"User-Agent": self.sess.user_agent,
"X-Requested-With": "XMLHttpRequest"
}
data = {
"Param": str(condition),
"parval": parval
}
while True:
r = self.sess.post(url=url, headers=headers, data=data)
t = r.text.replace('\\', '').replace('"[', '[').replace(']"', ']')
if len(t) <= 0:
raise NullContentError('Receive null content in reason_tree_content.')
elif t == '"remind"' or t == '"remind key"':
raise CheckCodeError('CheckCode Appeared in reason_tree_content.')
else:
break
json_data = json.loads(t)
tree_dict = {}
for type_data in json_data:
type_name = type_data['Key']
type_dict = {
'IntValue': type_data['IntValue'],
'ParamList': []
}
for data in type_data['Child']:
if data['IntValue']:
type_dict['ParamList'].append({'Key': data['Key'], 'IntValue': data['IntValue']})
tree_dict[type_name] = type_dict
if '法院层级' in tree_dict:
self.logger.critical('Error in reason tree content for condition: {}'.format(str(condition)))
return {'中级法院': {'ParamList': []}, '基层法院': {'ParamList': []}}
return tree_dict
def time_interval(self, condition: Condition, start_date: datetime = None):
"""
生成时间参数
"""
period_length = [90, 30, 10, 5, 1]
quark = len(period_length) - 1
period_loop = [4, 3, 3, 2, 5]
def split(start: datetime, period: int):
# 0 - 90天; 1 - 30天; 2 - 10天; 3 - 1天
for i in range(0, period_loop[period]):
s = start + timedelta(days=i * period_length[period])
e = start + timedelta(days=(i + 1) * period_length[period] - 1)
if start_date is None or e >= start_date:
info = self.tree_content(Parameter(param=str(condition.date(s, e)), sess=self.sess))['裁判年份']
if info['IntValue'] < MAX_PAGE * 20 or period == quark:
yield s, e, info['IntValue']
else:
self.logger.debug(
'{} {} {}'.format(s.strftime('%Y-%m-%d'), period + 1,
info['IntValue']))
yield from split(s, period + 1)
def tail_5_days(s, e):
info = self.tree_content(Parameter(param=str(condition.date(s, e)), sess=self.sess))['裁判年份']
if info['IntValue'] < MAX_PAGE * 20:
self.logger.debug(
'{} {} {}'.format(s.strftime('%Y-%m-%d'), e.strftime('%Y-%m-%d'),
info['IntValue']))
yield s, e, info['IntValue']
else:
cur = s
while cur <= e:
if start_date is None or cur >= start_date:
info = self.tree_content(Parameter(param=str(condition.date(s, e)), sess=self.sess))['裁判年份']
yield cur, cur, info['IntValue']
cur = cur + timedelta(days=1)
info = self.tree_content(Parameter(param=str(condition), sess=self.sess))['裁判年份']
if info['IntValue'] < MAX_PAGE * 20:
yield datetime(1990, 1, 1), datetime.today(), info['IntValue']
else:
for year in sorted(info['ParamList'], key=lambda item: int(item['Key']), reverse=False):
s = datetime(int(year['Key']), 1, 1)
e = datetime(int(year['Key']) + 1, 1, 1) - timedelta(days=1)
if start_date is None or e > start_date:
if year['IntValue'] > MAX_PAGE * 20:
self.logger.debug('{} {} {}'.format(s.strftime('%Y-%m-%d'), 0, info['IntValue']))
yield from split(s, 0)
yield from tail_5_days(s + timedelta(days=360), e)
else:
yield s, e, year['IntValue']
def district(self, condition: Condition, start_dist: str = None):
info = self.tree_content(Parameter(param=str(condition), sess=self.sess))['法院地域']
if start_dist is None:
start = True
else:
start = False
for d in sorted(info['ParamList'], key=lambda item: item['IntValue'], reverse=False):
dist = d['Key']
if not start:
if dist == start_dist:
start = True
yield dist
else:
yield dist
def court(self, condition: Condition, district: str, start_court: str = None):
"""
:param condition:
:param district:
:param start_court: start_court will only be available for level 2
:return: Court name, court level, court indicator, count
"""
level_count = {'高级法院': 0, '中级法院': 0, '基层法院': 0}
condition = condition.district(district)
info = self.tree_content(Parameter(param=str(condition), sess=self.sess))['法院层级']
satisfy = True
for item in info['ParamList']:
if item['IntValue'] > 200:
satisfy = False
if item['Key'] in level_count:
level_count[item['Key']] = item['IntValue']
if satisfy:
for idx, (k, v) in enumerate(level_count.items()):
if v > 0:
yield None, idx + 1, True, v
else:
start = start_court is None
if start and level_count['高级法院'] > 0:
yield None, 1, True, level_count['高级法院']
middle = self.court_tree_content(condition, parval=district)['中级法院']
for d in sorted(middle['ParamList'], key=lambda item: item['IntValue'], reverse=False):
mid_court = d['Key']
if not start:
if mid_court == start_court:
start = True
if start:
if 0 < d['IntValue'] < 200:
yield mid_court, 2, False, d['IntValue']
else:
base = self.court_tree_content(condition.court(mid_court, 2, False), parval=mid_court)['基层法院']
if d['IntValue'] - base['IntValue'] > 0:
yield mid_court, 2, True, d['IntValue'] - base['IntValue']
for g in sorted(base['ParamList'], key=lambda item: item['IntValue'], reverse=False):
base_court = g['Key']
if g['IntValue'] > 0:
yield base_court, 3, False, g['IntValue']