[Python] How to use BeautifulSoup find_all() to get a class with multiple classes?

Discussão em 'Python' iniciado por Stack, Junho 20, 2025.

  1. Stack

    Stack Membro Participativo

    I am not sure if the terminology "class with multiple classes" is correct but that is the best I can describe it.

    import requests
    from bs4 import BeautifulSoup

    url="https://curiosa.io/decks/cmbcn15x3003cl704euvw894m"
    response = requests.get(url)
    soup = BeautifulSoup(response.text, "html.parser")

    cards = soup.find_all(class_ = ["inline-block", "w-[30px]", "px-2"])
    #inline-block w-[30px] px-2
    print("length of cards: " + str(len(cards)))
    for card in cards:
    num = card.get_text()

    print(f"{num}")
    print("test")
    print("test")


    The cards object only has a length of 3, and card.get_text() does not print out a number. In the URL, at this block of code: <span class="inline-block w-[30px] px-2">1</span> I am expecting it to print 1. Currently the code should just be getting the number associated with each card on the webpage.

    I am not even sure if the multiple classes is the issue. It seems to not be rightly detecting this class though, which appears 35 times on the webpage: class="inline-block w-[30px] px-2"

    I tried cards = soup.find_all(class_ = ["inline-block", "w-[30px]", "px-2"]) to detect <span class="inline-block w-[30px] px-2">1</span> <span class="inline-block w-[30px] px-2">3</span> etc.

    but the list of cards only has a length of 3. Furthermore, it appears card.get_text() is not obtaining the text within the span or class.

    I have also tried cards = soup.find_all("span", class_ = ["inline-block", "w-[30px]", "px-2"]) This results is the length of cards being 0.

    The length of cards should be 35.

    Continue reading...

Compartilhe esta Página