Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting id names with beautifulsoup

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.

like image 972
Hairr Avatar asked Nov 18 '12 03:11

Hairr


Video Answer


1 Answers

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']
like image 144
Pattapong J Avatar answered Oct 24 '22 07:10

Pattapong J