问题描述
我正在尝试让BeautifulSoup搜索以下标签:
<a>
<span class="badge capita-voucher">
<span class="badge halal">
我应该如何在第一行代码中表达我的要求? 我在下面键入的代码是否合适?
for cell in row.find_all(['a', ("span", {"class":"badge capita-voucher"}), ("span", {"class":"badge halal"})]):
line_bs_object = BeautifulSoup(cell.__str__(), "html.parser")
csvRow.append(
line_bs_object.get_text().strip().replace('\\n', '').replace('\\r', '')
)
1楼
我可以通过传递给find_all
来find_all
:
def is_tag_interesting(tag):
is_interesting = False
if tag.name == 'a':
is_interesting = True
elif tag.name == 'span' and 'badge' in tag.class:
if any(s in tag.class for s in ('halal', 'capita-voucher')):
is_interesting = True
return is_interesting
for cell in row.find_all(is_tag_interesting):
...