我正在尝试让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', '')
)
答案 0 :(得分:0)
我会在passing a function as the filtering parameter之前处理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):
...