mingjun97的blog

日常填坑的各种记录

灯塔党建题库爬取代码

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
import urllib2
import json

url = "http://xxjs.dtdjzx.gov.cn/quiz-api/subject_info/randomList"
url_l = 'http://xxjs.dtdjzx.gov.cn/quiz-api/subject_info/list?chapterId=7j0d8qp4r2g28ogjt5hq0cbhne'

headers = {
"Host": "xxjs.dtdjzx.gov.cn",
"Accept": "*/*",
"version-code": "75",
"Accept-Language": "en-SG;q=1, zh-Hans-SG;q=0.9",
"Accept-Encoding": "gzip, deflate",
"device-name": "iPhone",
"system-version": "11.2.5",
"device-model": "iPhone",
"Connection": "close",
"version-name": "1.3.7",
"system-type": "iOS",
"User-Agent": "FuShengJiaXin/1.3.7 (iPhone; iOS 11.2.5; Scale/2.00)",
}

answers = dict()
request = urllib2.Request(url_l, None, headers)
response = urllib2.urlopen(request)
response = json.loads(response.read())
question_list = response['data']['subjectInfoList']

for t in range(1000):
request = urllib2.Request(url, None, headers)
response = urllib2.urlopen(request)
response = json.loads(response.read())
data = response['data']['subjectInfoList']
for item in data:
question = dict()
question['question'] = item['subjectTitle']
question['Right'] = []
options = item['optionInfoList']
for i in options:
if i['isRight'] == u'1':
question['Right'].append((i['optionType'], i['optionTitle'] + u' v'))
else:
question['Right'].append((i['optionType'], i['optionTitle']))
answers[json.dumps(question['question']).decode('unicode-escape')[1:-1]] = "A." + json.dumps(question['Right'][0][1]).decode('unicode-escape')[1:-1] + '\n' + \
"B." + json.dumps(question['Right'][1][1]).decode('unicode-escape')[1:-1] + '\n' + \
"C." + json.dumps(question['Right'][2][1]).decode('unicode-escape')[1:-1] + '\n' + \
"D." + json.dumps(question['Right'][3][1]).decode('unicode-escape')[1:-1]
if len(answers) == 200:
break
count = 1
for item in question_list:
print(str(count) + '.' + item['subjectTitle'])
print(answers[item['subjectTitle']])
count = count + 1

Comments