问题
我正在尝试从 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
我尝试使用的网址
Python HTML parsing with beautiful soup and filtering stop words
最佳答案
您可以使用分解()
:
http://www.crummy.com/software/BeautifulSoup/bs4/doc/#decompose
如果您只想清除文本或将元素从树中移除,请使用clear
和extract
(分解上面的描述)。
关于python - 使用 beautifulsoup 和 python 删除某些标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26233315/