【原创】Beautifulsoup如何只提取标签内的文本

示例

如下代码,只提取div里的文本,不提取span里的文本


from bs4 import BeautifulSoup

s='''
添加时间: " 26分钟前"
作者: " 陈冠希"
''' soup=BeautifulSoup(s,'html5lib') div=soup.find('div') print([text.strip() for text in div.find_all(text=True) if text.parent.name !='span' and text.strip()])

输出结果

你可能感兴趣的:(【原创】Beautifulsoup如何只提取标签内的文本)