from bs4 import BeautifulSoup from bs4 import element import re import logger def splittagElement(blocklist,childrenelement): if type(childrenelement) == element.NavigableString: block = { "type": "text", "value": childrenelement, "playTime": None, "param": None } blocklist.append(block) elif type(childrenelement) == element.Tag: for child in childrenelement.children: if child.name == "a": playTime = None if "playtime" in child.attrs: playTime = child.attrs["playtime"] if "url" in child.attrs: block = { "type": "audio", "value": child.attrs["url"], "playTime": playTime, "param": None } blocklist.append(block) elif child.name == "img": if "width" in child.attrs and "height" in child.attrs: width=child.attrs["width"] height=child.attrs["height"] elif "style" in child.attrs: style=child.attrs["style"] width=re.search("width:([^;]*)",style).group(1) height=re.search("height:([^;]*)",style).group(1) else: width=None height=None if width or height: param={ "width":width, "height":height } else: param=None block = { "type": "image", "value": child.attrs["src"], "playTime": None, "param":param } blocklist.append(block) else: splittagElement(blocklist,child) def splitQuestionBody(questionBody): """ 切分quesitonBody """ sectionlist=[] bodyhtml=BeautifulSoup(questionBody,features="html.parser") plist=bodyhtml.find_all("p") for p in plist: #遍历p标签 section={ "blocks":[] } splittagElement(section["blocks"],p) sectionlist.append(section) return sectionlist def splitQuesitonAnswer(quesitonanswer): """ 切分问题标答 """ sectionlist = [] #print(quesitonanswer) if quesitonanswer: bodyhtml = BeautifulSoup(quesitonanswer, features="html.parser") plist = bodyhtml.find_all("p") for p in plist: # 遍历p标签 section = { "blocks": [] } splittagElement(section["blocks"], p) sectionlist.append(section) else: sectionlist=None return sectionlist def splitQuesitonStudentAnswer(examrecordid,studentanswer,answerType,questionType): """ 切分学生答案 """ #logger.info("examrecordid is %s,questionType is %s,answerType is %s,studentanswer is %s"%(str(examrecordid),questionType,answerType,studentanswer)) if studentanswer: sectionlist = [] if questionType=="ESSAY": if answerType=="SINGLE_AUDIO": answer={ "blocks":[{ "type": "audio", "value": studentanswer, "playTime": None, "param": None }]} sectionlist.append(answer) else: #取图片 bodyhtml = BeautifulSoup(studentanswer, features="html.parser") blocks={ "blocks":[] } for body in bodyhtml: if type(body) == element.NavigableString: textanswer = { "type": "text", "value": body, "playTime": None, "param": None } blocks["blocks"].append(textanswer) elif type(body)!=element.Comment: alist = body.find_all("a") if len(alist)>0: for atag in alist: imageanswer={ "type": "image", "value": atag.attrs["href"], "playTime": None, "param": { "width": 200, "height": 200 } } blocks["blocks"].append(imageanswer) else: textanswer = { "type": "text", "value": body.text, "playTime": None, "param": None } blocks["blocks"].append(textanswer) sectionlist.append(blocks) elif questionType=="FILL_UP": stuanswer="" for stu in studentanswer.split("##"): if stu =="": stuanswer=stuanswer+","+" " else: stuanswer=stuanswer+","+stu answer = { "blocks":[{ "type": "text", "value": stuanswer[1:], "playTime": None, "param": None }] } sectionlist.append(answer) else: answer = { "blocks": [{ "type": "text", "value": studentanswer, "playTime": None, "param": None }] } sectionlist.append(answer) print(sectionlist) else: sectionlist = None return sectionlist