[这是前端的内容:]
<h1>hello</h1>
<span>123</span>
<h1>hello</h1>
<span>123</span>
<h1>hello</h1><span>123</span>
<script>alert(123)</script>
<script>alert(123)</script>
<script>alert(123)</script>
[后端解析到的是:]
<pre><span style="color:#6a8759;"><h1>hello</h1><span>123</span> </span><span style="color:#6a8759;"><h1>hello</h1><span>123</span> </span><span style="color:#6a8759;"> </span><span style="color:#6a8759;"><h1>hello</h1><span>123</span> </span><span style="color:#6a8759;"> </span><span style="color:#6a8759;"> </span><span style="color:#6a8759;"><script>alert(123)</script> </span><span style="color:#6a8759;"><script>alert(123)</script> </span><span style="color:#6a8759;"><script>alert(123)</script></span></pre>
[后端代码是:]
if request.method=="POST":
title=request.POST.get('title')
content=request.POST.get("content")
#拿到标签字符串
soup = BeautifulSoup(content, "html.parser",fromEncoding="UTF-8")
#先过滤:
for tag in soup.find_all():
if tag.name == "script" : #####问题出在这里,哪怕我换成”</script>“,也匹配不到这里的 if
print("script--------->",tag.name) #####所以压根走不到这里,我懵了,555 怎样变回 script 呀??
tag.decompose() # 从 soup 里吧这个 script 的 tag 删除掉
desc = soup.text[0:150] #对文本进行截断 #######这里就变回了 script,怎么让前面的就变回 script ??
<h1>hello</h1>
<span>123</span>
<h1>hello</h1>
<span>123</span>
<h1>hello</h1><span>123</span>
<script>alert(123)</script>
<script>alert(123)</script>
<script>alert(123)</script>
[后端解析到的是:]
<pre><span style="color:#6a8759;"><h1>hello</h1><span>123</span> </span><span style="color:#6a8759;"><h1>hello</h1><span>123</span> </span><span style="color:#6a8759;"> </span><span style="color:#6a8759;"><h1>hello</h1><span>123</span> </span><span style="color:#6a8759;"> </span><span style="color:#6a8759;"> </span><span style="color:#6a8759;"><script>alert(123)</script> </span><span style="color:#6a8759;"><script>alert(123)</script> </span><span style="color:#6a8759;"><script>alert(123)</script></span></pre>
[后端代码是:]
if request.method=="POST":
title=request.POST.get('title')
content=request.POST.get("content")
#拿到标签字符串
soup = BeautifulSoup(content, "html.parser",fromEncoding="UTF-8")
#先过滤:
for tag in soup.find_all():
if tag.name == "script" : #####问题出在这里,哪怕我换成”</script>“,也匹配不到这里的 if
print("script--------->",tag.name) #####所以压根走不到这里,我懵了,555 怎样变回 script 呀??
tag.decompose() # 从 soup 里吧这个 script 的 tag 删除掉
desc = soup.text[0:150] #对文本进行截断 #######这里就变回了 script,怎么让前面的就变回 script ??