是否可以找到多个带有条件的标签?
<a href = "/img/something.jpg">
<img src= "/img/somethingelse.png">
我可以说
找到包含“ / img /”的所有“ a”和“ img”标签
答案 0 :(得分:0)
是的,仅向find_all()
方法提供函数(可以是lambda函数):
data = """<a href = "/img/something.jpg">
<img src= "/img/somethingelse.png">"""
from bs4 import BeautifulSoup
soup = BeautifulSoup(data, 'lxml')
for tag in soup.body.find_all(lambda t: t.name in ('a', 'img') and \
('href' in t.attrs and '/img/' in t['href']) or
('src' in t.attrs and '/img/' in t['src'])):
print(tag.name, tag.attrs)
print('*' * 80)
输出:
a {'href': '/img/something.jpg'}
********************************************************************************
img {'src': '/img/somethingelse.png'}
********************************************************************************