'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 :
<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:
From this I want in my code to output the span 'No 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 1:[1]
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Invizi |

