Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get span title using beautifulsoup

I have this span and I want to get the title

<span title="Something"></span>

How to get that with beautifulsoup?

res = soup.find('span')
print res //Was trying to add res.title but result is 'None'
like image 561
a1204773 Avatar asked Oct 26 '25 22:10

a1204773


1 Answers

You should be able to access it like this:

res = soup.find('span')['title']

Docs

Edit: I shoudl clarify, res would then be the value of the title attribute. If you want the element to use later, change my code to:

res = soup.find('span')
title = res['title']

Then you could keep using res (if needed).

Also, .find is going to return a single element. You'll want to make sure it is the span you want, since the HTML could have more than one span.

like image 86
Gromer Avatar answered Oct 28 '25 13:10

Gromer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!