Python 是否仅查找属性包含子字符串的元素?这可能吗?

Python 是否仅查找属性包含子字符串的元素?这可能吗?,python,html,beautifulsoup,html-parsing,Python,Html,Beautifulsoup,Html Parsing,我在我的BeautifulSoupcode中调用了find_all()。目前,这可以获取所有图像,但如果我只想针对在其src中有子字符串“占位符”的图像,我该怎么做 for t in soup.find_all('img'): # WHERE img.href.contains("placeholder") 您可以在src关键字参数中: for t in soup.find_all('img', src=lambda x: x and 'placeholder' in x): 或者,a:

我在我的
BeautifulSoup
code中调用了
find_all()
。目前,这可以获取所有图像,但如果我只想针对在其
src
中有子字符串“占位符”的图像,我该怎么做

for t in soup.find_all('img'):  # WHERE img.href.contains("placeholder")
您可以在
src
关键字参数中:

for t in soup.find_all('img', src=lambda x: x and 'placeholder' in x):
或者,a:

或者,不要使用
find_all()
,而是使用:


或者使用CSS选择器:
soup.select('img[src*=占位符])
@MartijnPieters我也会这么做,谢谢:)我会把它包含在答案中。你大概是指
src
属性,而不是
href
属性?
import re

for t in soup.find_all('img', src=re.compile(r'placeholder')):
for t in soup.select('img[src*=placeholder]'):