简体   繁体   English

带有OR和空类的BeautifulSoup FindAll

[英]BeautifulSoup FindAll with OR and Empty Class

I am trying to extract the following: 我正在尝试提取以下内容:

<td class="indent">Interest Income on Fed. Funds</td>
<td class="">-</td>

with this 有了这个

interest=a.findAll("td",{'class':[''|'indent']},limit=6)

However, it returns 但是,它返回

TypeError: unsupported operand type(s) for |: 'str' and 'str' TypeError:|:'str'和'str'不支持的操作数类型

How could I find empty class OR class_='indent'? 如何找到空类或class _ ='indent'?

Try this: 尝试这个:

interest = a.findAll("td", {'class': [None, 'indent']}, limit=6)
  1. Pass multiple classes as a list (with , separator not with | ) 将多个类作为列表传递(使用,分隔符不使用|
  2. Select empty classes with None 选择None空类

Another option as well for bs4: bs4的另一个选择:

def empty_or_indent(css_class):
    return css_class is None or css_class is 'indent'   

a.find_all('td', class_=empty_or_indent, limit=6)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM