我正在使用Python / beautifulSoup查找特定类的div,我想从文件中删除整个html元素。
这就是我所拥有的-
with open(url) as f:
elementToDelete = BeautifulSoup(f.read()).find("div", {'class': 'element-that-needs-to-go'})
removeTheElement = elementToDelete.replace('THISISWHEREIMSTUCK', '')
with open(url, 'w') as f:
f.write(removeTheElement)
我似乎找不到合适的方法来做自己想做的事。
答案 0 :(得分:1)
使用分解方法:
Python代码:
from bs4 import BeautifulSoup
html = '''
<div>
<div class="element-that-needs-to-go">
</div>
</div>
'''
soup = BeautifulSoup(html)
tag_to_remove = soup.find("div", {'class': 'element-that-needs-to-go'})
tag_to_remove.decompose()
print(soup)
演示: Here