[Python] bs4 cannot extract text from an element

Discussão em 'Python' iniciado por Stack, Março 29, 2025.

  1. Stack

    Stack Membro Participativo

    [​IMG]

    import requests
    from bs4 import BeautifulSoup

    url = 'https://www.tori.fi/recommerce/forsale/item/22362242'
    headers = {"User-Agent": "Mozilla/5.0"}

    response = requests.get(url, headers=headers)

    if response.status_code == 200:
    soup = BeautifulSoup(response.text, "html.parser")

    section = soup.find("section", class_="flex gap-8 flex-wrap mb-24")
    #print(section)

    items = section.find_all("p", class_="mb-0")
    print(items)

    hrefs = [b["mb-0"] for item in items if (a := items.find("b", class_="mb-0"))]
    print(hrefs)


    So without the hrefs part I get what I have attached as an image.

    I have tried to extract 'Hyvä' and 'Näytönohjaimet' from here using different methods. Can someone pls guide me on how to get this data from here.

    Continue reading...

Compartilhe esta Página