在" find_all"中添加1个以上的标准用于搜索HTML代码

时间:2015-10-25 04:36:58

标签: python beautifulsoup bs4

我正在尝试让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 个答案:

答案 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):
    ...