如何从发送到beautifulSoup类的文件中删除html元素?

时间:2019-10-17 00:33:34

标签: python methods beautifulsoup

我正在使用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)

我似乎找不到合适的方法来做自己想做的事。

1 个答案:

答案 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