123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183 |
- 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
|