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

问题描述 投票:0回答:1

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

我似乎找不到正确的方法来完成我想要的事情。

python methods beautifulsoup
1个回答
0
投票

使用分解方法:

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)

Demo: Here

© www.soinside.com 2019 - 2024. All rights reserved.