我正在尝试对 MS FrontPage 生成的网站的 html 进行“defrontpagify”,并且正在编写一个 BeautifulSoup 脚本来执行此操作。
但是,我陷入了尝试从包含它们的文档中的每个标签中去除特定属性(或列表属性)的部分。代码片段:
REMOVE_ATTRIBUTES = ['lang','language','onmouseover','onmouseout','script','style','font',
'dir','face','size','color','style','class','width','height','hspace',
'border','valign','align','background','bgcolor','text','link','vlink',
'alink','cellpadding','cellspacing']
# remove all attributes in REMOVE_ATTRIBUTES from all tags,
# but preserve the tag and its content.
for attribute in REMOVE_ATTRIBUTES:
for tag in soup.findAll(attribute=True):
del(tag[attribute])
它运行没有错误,但实际上并没有剥离任何属性。当我在没有外部循环的情况下运行它时,只需硬编码单个属性(soup.findAll('style'=True),它就可以工作。
任何人都知道这里的问题吗?
PS - 我也不太喜欢嵌套循环。如果有人知道更实用的地图/过滤器风格,我很乐意看到它。