0

我正在尝试让 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', '')
                )
4

1 回答 1

0

我会通过将函数作为过滤参数传递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):
    ...
于 2015-10-25T07:06:26.577 回答