【发布时间】:2014-02-09 03:01:15
【问题描述】:
我正在尝试从源中删除样式标签及其内容,但它不起作用,没有错误只是不会分解。这就是我所拥有的:
source = BeautifulSoup(open("page.html"))
getbody = source.find('body')
for child in getbody[0].children:
try:
if child.get('style') is not None and child.get('style') == "display:none":
# it in here
child.decompose()
except:
continue
print source
# display:hidden div's are still there.
【问题讨论】:
-
您的语法无效;没有
except处理程序。如果您使用except: pass删除try/except来查看您正在掩盖的任何错误。 -
getbody[0]也会引发KeyError。 -
我不知道这段代码怎么不会抛出任何
SyntaxError。
标签: python beautifulsoup