Skip to content
This repository was archived by the owner on Jun 3, 2025. It is now read-only.

Commit ad11015

Browse files
committed
feat:对编程题中答案存在<></>标签进行优化✨
1 parent 38d380d commit ad11015

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

my_xxt/api.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,7 @@ def get_question(self, work_url: str, params=None) -> list:
452452
# 判断选项是否是乱序的
453453
randomOptions = re.findall(r'value="(.*?)"', str(randomOptions_soup))[0]
454454
self.randomOptions = randomOptions
455-
455+
456456
work_view = work_view_soup.find_all("div", attrs={"class": "padBom50 questionLi"})
457457
_question_type = QuestionType()
458458
for item in work_view:
@@ -475,6 +475,19 @@ def selectFunc(type_question: str, _type: list) -> str:
475475
return item["fun"]
476476
return "error"
477477

478+
@staticmethod
479+
def escape_tags(html_str: str):
480+
# 判断是否有html标签
481+
if bool(BeautifulSoup(html_str, "html.parser").find()):
482+
# 匹配html标签的正则表达式
483+
pattern = re.compile(r'<.+?>')
484+
485+
# 将匹配到的html标签进行转义,但不包含标签中的内容
486+
escaped_str = pattern.sub(lambda m: m.group(0).replace("<", "&lt;").replace(">", "&gt;"), html_str)
487+
return escaped_str
488+
else:
489+
return html_str
490+
478491
@staticmethod
479492
def allQuestionId(answer: list) -> str:
480493
"""
@@ -548,7 +561,7 @@ def create_from_data(self, commit_from: dict, answer: list) -> dict:
548561
from_date_2[key4] = len(item["answer"])
549562
for answer_item in range(1, len(item["answer"]) + 1):
550563
_key = "answerEditor" + item["id"] + str(answer_item)
551-
from_date_2[_key] = "<p>" + item["answer"][answer_item - 1].split(")")[1] + "<br/></p>"
564+
from_date_2[_key] = "<p>" + item["answer"][answer_item - 1][3:] + "<br/></p>"
552565
elif "判断题" in item["title"]:
553566
key5 = "answertype" + item["id"]
554567
from_date_2[key5] = 3
@@ -580,7 +593,7 @@ def create_from_data(self, commit_from: dict, answer: list) -> dict:
580593
from_date_2[key12] = len(item["answer"])
581594
for answer_item in range(1, len(item["answer"]) + 1):
582595
_key = "answerEditor" + item["id"] + str(answer_item)
583-
from_date_2[_key] = "<p>" + item["answer"][answer_item - 1].split(")")[1] + "<br/></p>"
596+
from_date_2[_key] = "<p>" + self.escape_tags(item["answer"][answer_item - 1][3:]) + "<br/></p>"
584597
elif "其他" in item['title']:
585598
key13 = "answertype" + item["id"]
586599
from_date_2[key13] = 8

0 commit comments

Comments
 (0)