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
您可以使用decompose():http :
//www.crummy.com/software/BeautifulSoup/bs4/doc/#decompose
如果您只想清除文本或将元素从树中删除,请使用clear和extract(分解上方的描述)。