使用beautifulsoup和python删除某些标签

use*_*459 1 html python strip beautifulsoup

我正在尝试从BeautifulSoup下载的html文件中删除类似<h2>和的样式标签<div class=...>。我确实想保留标签包含的内容(例如文本),但是这似乎不起作用。

我尝试过的

for url in urls:
    response = requests.get(url, headers=headers)
    soup = BeautifulSoup(response.content, 'html.parser')
    table = soup.find("div", {"class": "product_specifications bottom_l js_readmore_content"})
    print "<hr style='border-width:5px;'>"
    for style in table.find_all('style'):
        if 'style' in style.attrs:
            del style.attrs['style']
    print table
Run Code Online (Sandbox Code Playgroud)

我尝试过的Urls

Python HTML解析,包含漂亮的汤和过滤停用词

使用Python和lxml从HTML删除类属性

BeautifulSoup标签去除

m.w*_*ski 5

您可以使用decompose()http : //www.crummy.com/software/BeautifulSoup/bs4/doc/#decompose

如果您只想清除文本或将元素从树中删除,请使用clearextract(分解上方的描述)。

  • 所以请在问题中解释它,而不是我评论 (2认同)