做靓汤把柄玩得好

2024-04-25 22:57:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在制作一个脚本来重构模板增强的html。你知道吗

我希望beautifulsoup能够逐字打印{{}}中的任何代码,而不将>或其他符号转换为html实体。你知道吗

它需要将一个包含多个模板的文件拆分为多个文件,每个文件包含一个模板。你知道吗

规范:splitTemplates templates.html templateDir必须:

  1. 阅读模板.html你知道吗
  2. 对于每个<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 }}变为{{ &gt; directive }},但需要作为{{ > directive }}保存

更新:f.write(t.prettify(formatter=None))有时保留>,有时将其更改为&gt;。这似乎是最明显的变化,不知道为什么它改变了一些而不是其他。你知道吗

解决:感谢海武和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()

另见:https://gist.github.com/DrPaulBrewer/9104465


Tags: 文件name模板htmltemplateopenprettifywrite