Reputation: 7043
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'
Upvotes: 4
Views: 9927
Reputation: 9931
You should be able to access it like this:
res = soup.find('span')['title']
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.
Upvotes: 13
Reputation: 1770
This is what the documentation has:
soup.findAll(['title', 'p'])
# [<title>Page title</title>,
# <p id="firstpara" align="center">This is paragraph <b>one</b>.</p>,
# <p id="secondpara" align="blah">This is paragraph <b>two</b>.</p>]
soup.findAll({'title' : True, 'p' : True})
# [<title>Page title</title>,
# <p id="firstpara" align="center">This is paragraph <b>one</b>.</p>,
# <p id="secondpara" align="blah">This is paragraph <b>two</b>.</p>]
You could also use a Regex.
Upvotes: 0