我正在使用Django编写博客应用程序。 我希望启用评论作者使用一些标签(如<strong>
,a
等),但禁用所有其他标签。
此外,我想让他们在<code>标签中放置代码,并让pygments解析它们。
例如,有人可能会编写以下评论:
I like this article, but the third code example <em>could have been simpler</em>:
<code lang="c">
#include <stdbool.h>
#include <stdio.h>
int main()
{
printf("Hello World\n");
}
</code>
问题是,当我使用BeautifulSoup解析注释以剥离不允许的HTML标签时,它也会解析<code>块的内容,并将<stdbool.h>和<stdio.h>视为HTML标签。
我该如何告诉BeautifulSoup不要解析<code>块?也许有其他更适合这项工作的HTML解析器?