我在我的 BeautifulSoup
代码中调用了 find_all()
。这目前可以获取所有图像,但如果我只想定位在其 src
中具有“占位符”子字符串的图像,我该怎么做?
for t in soup.find_all('img'): # WHERE img.href.contains("placeholder")
最佳答案
您可以 pass a function在 src
关键字参数中:
for t in soup.find_all('img', src=lambda x: x and 'placeholder' in x):
或者,一个 regular expression :
import re
for t in soup.find_all('img', src=re.compile(r'placeholder')):
或者,不使用 find_all()
,而是使用 select()
:
for t in soup.select('img[src*=placeholder]'):
关于python - BeautifulSoup 只查找属性包含子字符串的元素?这可能吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28240956/