If I had the text:
text = '<span id="foo"></span> <div id="bar"></div>'
with text that can change (that might not have any ids), how could I use BeautifulSoup to get the id names regardless of tag name(returning ['foo','bar']). I'm not that experienced to BeautifulSoup and have been confused on doing this task.
You need to get tag with id attributes then return values of id attributes to string e.g.
from BeautifulSoup import BeautifulSoup
text = '<span id="foo"></span> <div id="bar"></div>'
pool = BeautifulSoup(text)
result = []
for tag in pool.findAll(True,{'id':True}) :
result.append(tag['id'])
and result
>>> result
[u'foo', u'bar']
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With