【发布时间】:2011-04-29 18:38:41
【问题描述】:
我正在用 Django 编写一个博客应用程序。我想让评论作者使用一些标签(如<strong>、a 等),但禁用所有其他标签。
另外,我想让他们把代码放在标签里,让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 标签时,它还会解析 块的内部,并将 <stdbool.h> 和 <stdio.h> 视为 HTML 标签.</stdio.h></stdbool.h>
如何告诉 BeautifulSoup 不要解析 块?也许还有其他 HTML 解析器更适合这项工作?
【问题讨论】:
-
请参阅下面的参考资料。这可以解决您面临的相同问题。
标签: python html django beautifulsoup pygments