[Python] Extract strings when class names are repeated (BeautifulSoup)

Discussão em 'Python' iniciado por Stack, Fevereiro 12, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to extract two strings from the HTML soup below, which comes from https://store.steampowered.com/app/2622380/ELDEN_RING_NIGHTREIGN/

    In particular I want to extract "FromSoftware, Inc." and "Bandai Namco Entertainment" that show up under the Publisher label

    Here is the HTML. I know it's a bit long, but it's all needed to reproduce the error I get

    <div class="glance_ctn_responsive_left">
    <div id="userReviews" class="user_reviews">
    <div class="user_reviews_summary_row" onclick="window.location='#app_reviews_hash'" style="cursor: pointer;" data-tooltip-html="No user reviews" itemprop="aggregateRating" itemscope="" itemtype="http://schema.org/AggregateRating">
    <div class="subtitle column all">All Reviews:</div>
    <div class="summary column">No user reviews</div>
    </div>
    </div>
    <div class="release_date">
    <div class="subtitle column">Release Date:</div>
    <div class="date">2025</div>
    </div>
    <div class="dev_row">
    <div class="subtitle column">Developer:</div>
    <div class="summary column" id="developers_list">
    <a href="https://store.steampowered.com/curator/45188208?snr=1_5_9__2000">FromSoftware, Inc.</a>
    </div>
    </div>
    <div class="dev_row">
    <div class="subtitle column">Publisher:</div>
    <div class="summary column">
    <a href="https://store.steampowered.com/curator/45188208?snr=1_5_9__2000">FromSoftware, Inc.</a>, <a href="https://store.steampowered.com/curator/45188208?snr=1_5_9__2000">Bandai Namco Entertainment</a>
    </div>
    <div class="more_btn">+</div></div>
    </div>


    I'm running this script

    from bs4 import BeautifulSoup

    publisher = soup.find('div', class_='dev_row')
    publisher_name = publisher.text.strip() if publisher else "N/A"

    print(publisher_name)


    The issue I have is that I cannot use what I would normally use to identify the strings:

    • The class "dev_row" is repeated twice in the soup, so I cannot use it
    • The tag "a" is repeated twice in the soup
    • I cannot use the links, as I am running this script on multiple pages and the link changes each time

    Note that I am very new at this, so I might be missing something very obvious

    Continue reading...

Compartilhe esta Página