如何在 BeautifulSoup 的 .find_all 中使用连字符?

How to use hyphen inside .find_all of BeautifulSoup?

我正在尝试收集 span 内的文本值,在名为 Full Time:

的情况下
<div id="bottomContainer">
  <div class="tableWrapper">
    <div class="clockWrapper">
      <span data-push="clock">Full Time</span>

但是在尝试使用时:

soup.find_all("span", data-push="clock")

逻辑上 returns 由于连字符在 data-push 中请求参数时出错,在这种情况下我应该如何处理?

您只能使用有效的 python 变量名作为关键字参数的名称,即 data-push 是一个表达式,没有有效的变量名。在这种情况下使用

soup.find_all("span", attrs={'data-push': 'clock'})

相反。

另一种选择是使用 CSS 选择器:

print(soup.select('[data-push="clock"]'))