0
我想写一个脚本,它将从Python中的简单html文件中删除所有图像,并将文件保存到位。Python条形文件的图像标签
这里是我的尝试:
from bs4 import BeautifulSoup, NavigableString
def strip_tags(html, invalid_tags):
soup = BeautifulSoup(html)
for tag in soup.findAll(True):
if tag.name in invalid_tags:
s = ""
for c in tag.contents:
if not isinstance(c, NavigableString):
c = strip_tags(unicode(c), invalid_tags)
s += unicode(c)
tag.replaceWith(s)
return soup
data ="C:\\Users\\ADMIN\\Documents\\webpage 1.htm"
with open(data) as orig_f:
html = BeautifulSoup(orig_f.read())
invalid_tags = ['img']
print orig_f
print strip_tags(orig_f, invalid_tags)
有两件事情,我挣扎,首先所有的代码运行没有错误,但最后一行,打印,打印什么,前行打印一个内存地址。我试过寻找我的问题/阅读文件,以了解我在这里做错了什么,但我正在挣扎。
如何让此代码从我的HTML文件中删除所有图像标记,以及将其保存到位的最佳做法是什么?