[Python] How to find all <a href> with parent <h4 class='title'> using BeautifulSoup

Discussão em 'Python' iniciado por Stack, Maio 15, 2025.

  1. Stack

    Stack Membro Participativo

    I am searching how to find all the href having a h4 class='title' parent, using BeautifulSoup. There is a lot of html tags, including sometime other a, between each h4 class='title' and there is also other tags with a class='title'.

    I tried that but of course, the line in comment is not valid...

    soup = BeautifulSoup(htmlfile.read_text(), 'html.parser')
    booktitles = soup.find_all("h4", class_="title")
    # booklinks = booktitles.find_all('a')


    I also tried that but there is an error...

    soup = BeautifulSoup(htmlfile.read_text(), 'html.parser')
    booklinks = [h4.find_all("a") for h4 in soup.find_all("h4", class_="title")]


    Any ideas? Thanks!

    Continue reading...

Compartilhe esta Página