Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Get span text using bs4 with python

What am I doing wrong here?

Problematic code:

page = requests.get(link).content
    soup = bs(page, "html.parser")
    
    element = soup.find('span', class_ = 'value')
    try:
      print(element)
    except:
      print('does not work..')

Well this are the highlighted lines for the specific block of message :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

<section _ngcontent-tyn-c55="" class="tr-section">
    <!---->
    <div _ngcontent-tyn-c55="" class="body">
        <data-tile _ngcontent-tyn-c265="" trtitle="Current status" class="tr-gutter success light" _nghost-tyn-c85="">
            <div _ngcontent-tyn-c85="" role="heading" aria-level="3" class="title">Current status</div>
            <div _ngcontent-tyn-c85="" role="figure" class="value">
                <i _ngcontent-tyn-c85="" aria-label="figure icon" class="material-icons ng-star-inserted">check_circle</i>
                <!---->
                <span _ngcontent-tyn-c85="" aria-label="figure value">No unsafe content found</span>
            </div>
            <!---->
        </data-tile>
        <column-layout _ngcontent-tyn-c265="" _nghost-tyn-c54="">
            <!---->
            <div _ngcontent-tyn-c265="" class="ng-star-inserted">
                <h3 _ngcontent-tyn-c265="">Site info</h3>
                <p _ngcontent-tyn-c265="">This info was last updated on Feb 18, 2022.</p>
                <p _ngcontent-tyn-c265="">Site safety can change over time. Check back for updates.</p>
            </div>
            <!---->
        </column-layout>
    </div>
</section>

And an image with it:

Block of the output message

From this I want in my code to output the spanNo unsafe content found

How could I do this, I’d prefer directly the correct code, if possible, because that way I could understand it better.

Thank you,

RVZWN.

>Solution :

Your HTML isn’t that clear. Here’s a general way of doing it with the information you have given.

# send request to page
res = requests.get(link)

# check if the response failed
if res.status_code == 200:
    soup = BeautifulSoup(res.text, "html.parser")
    # Use select and select_one which allow you to use css selectors.
    message = soup.select_one(".body span").text
    print(message)
else:
    print("request to", link, "failed")

If you don’t know how to use CSS selectors, there are some examples in https://devhints.io/css

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading