This is the first post
This is the second post
This is the third post
This is the fourth post
使用Beautifulsoup 4.7.0及更高版本,并使用CSS选择器中的':contains()'伪类代替':-soup-contains'。示例代码如下:
from bs4 import BeautifulSoup
html = '''
This is the first post
This is the second post
This is the third post
This is the fourth post
soup = BeautifulSoup(html, 'html.parser') posts = soup.select('.content p:contains("second")') print(posts) # 输出结果为:[
This is the second post
]