-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
62 lines (55 loc) · 2.15 KB
/
server.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
from flask import Flask,request,Response,jsonify,render_template,abort
import json
import pandas as pd
from flask_cors import CORS
import os
import re
pattern=re.compile(r'\W*',flags=re.UNICODE)
app=Flask(
__name__,static_folder='./source/static\\',template_folder='./source/templates\\'
)
CORS(app,supports_credentials=True)
@app.route('/')
def hello_world():
# return 'Hello, my dear guest!'
return render_template('./index.html')
@app.route('/search',methods=['POST'])
def search():
course = re.sub(pattern, '', request.json['course'])
# unit=request.json['unit'].replace('\\','').replace('/','').replace('\xa0',' ')
unit = re.sub(pattern, '', request.json['unit'])
problems=request.json['problems']
query=pd.DataFrame(problems)
for i in range(len(query)):
query.iloc[i, 0] = ''.join(query.iloc[i, 0].split())
print(query)
print('接收到'+str(len(query))+'个题目')
answers=pd.read_json('./source/scrap_results/'+course+'/'+str(unit)+'.json',encoding='utf-8')
res_answer=pd.merge(query, answers,how='inner',on='caption')
res_answer.to_csv('answer.csv',',',header=False,encoding='utf-8')
return 'ok'
@app.route('/result',methods=['GET'])
def show_result():
if not os.path.exists('answer.csv'):
abort(401)
results=pd.read_csv('answer.csv',encoding='utf-8')
# results=result.values.tolist()
# print(results)
res_result=[]
for i in range(len(results)):
res={}
res['caption']=results.iloc[i,1]
res['answer'] = results.iloc[i, 2].replace('spans', 'span s').replace('imgs','img s')
# res['answer'] = results.iloc[i, 1]
res['analysis']=results.iloc[i,3]
res_result.append(res)
# print(result)
os.remove('answer.csv')
return render_template('search_result.html',questions=res_result)
@app.errorhandler(401)
def page_error(error):
# 返回元组,若没有第二个401,则默认为200
# return render_template('error.html', error_info=error), 401
return "尚未对该门课程或该章节进行爬取,或者需要重新运行页面脚本",401
if __name__=='__main__':
app.run(debug=True,host='localhost',port='80')