Search code examples
pythonbeautifulsoupfindall

How to use hyphen inside .find_all of BeautifulSoup?


I'm trying to collect the text value that is inside span, in the case called Full Time:

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

But when trying to use:

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

Logically returns an error asking for a parameter in data-push because of the hyphen, how should I proceed in cases like this?


Solution

  • You can only use valid python variable names as names of keyword arguments, i.e. data-push is an expression and no valid variable name. In this case use

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

    instead.