[Python] Getting width/attributes out of a tag in Beautifulsoup instead of text

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

  1. Stack

    Stack Membro Participativo

    So the beautifulsoup documentation I can find talks about finding a specific tag using id, class etc... But it doesn't talk about how to extract data from within the tag rather than what it surrounds.

    My issue:

    <img src=yellowbar.png width=63.94 height=10><img src=redbar.png width=36.0632181423 height=10><br />
    Power:</b> 1480 / 1480<br />
    <img src=yellowbar.png width=100 height=10><img src=redbar.png width=0 height=10><br />


    I have this HTML. There are around a total of 20 tags on the page, of which 3 have src=yellowbar.png

    my goal is, to select the second one, and get the width back. So I am guessing it would go:

    Find tags -> find src=yellowbar.png -> select second one -> print width back.

    How would I go about this?

    So far I've managed to print a list of all tags.

    soup = BeautifulSoup(element, "lxml")

    tag = soup.find_all('img')
    print(tag)


    which returns

    [<img height="10" src="yellowbar.png" width="77"/>, <img height="10" src="redbar.png" width="0"/>]

    Continue reading...

Compartilhe esta Página