我正在制作一个脚本来重构模板增强的html。你知道吗
我希望beautifulsoup能够逐字打印{{
}}
中的任何代码,而不将>
或其他符号转换为html实体。你知道吗
它需要将一个包含多个模板的文件拆分为多个文件,每个文件包含一个模板。你知道吗
规范:splitTemplates templates.html templateDir
必须:
<template name="xxx">contents {{ > directive }}</template>
,将此模板写入文件templateDir/xxx
代码段:
soup = bs4.BeautifulSoup(open(filename,"r"))
for t in soup.children:
if t.name=="template":
newfname = dirname+"/"+t["name"]+ext
f = open(newfname,"w")
# note f.write(t) fails as t is not a string -- prettify works
f.write(t.prettify())
f.close()
不良行为:
{{ > directive }}
变为{{ > directive }}
,但需要作为{{ > directive }}
保存
更新:f.write(t.prettify(formatter=None))
有时保留>
,有时将其更改为>
。这似乎是最明显的变化,不知道为什么它改变了一些而不是其他。你知道吗
解决:感谢海武和https://stackoverflow.com/a/663128/103081
import HTMLParser
U = HTMLParser.HTMLParser().unescape
soup = bs4.BeautifulSoup(open(filename,"r"))
for t in soup.children:
if t.name=="template":
newfname = dirname+"/"+t["name"]+ext
f = open(newfname,"w")
f.write(U(t.prettify(formatter=None)))
f.close()
您需要取消显示HTML内容。为此,请使用HTMLParser模块。这不是我最初的想法。我是从:
How can I change '>' to '>' and '>' to '>'?
相关问题 更多 >
编程相关推荐