Beautiful soup getting tag.id

I am trying to get a list of div ids from a page. When I print out the attributes, I get the identifiers listed.

for tag in soup.find_all(class_="bookmark blurb group") : print(tag.attrs) 

leads to:

 {'id': 'bookmark_8199633', 'role': 'article', 'class': ['bookmark', 'blurb', 'group']} {'id': 'bookmark_7744613', 'role': 'article', 'class': ['bookmark', 'blurb', 'group']} {'id': 'bookmark_7338591', 'role': 'article', 'class': ['bookmark', 'blurb', 'group']} {'id': 'bookmark_7338535', 'role': 'article', 'class': ['bookmark', 'blurb', 'group']} {'id': 'bookmark_4530078', 'role': 'article', 'class': ['bookmark', 'blurb', 'group']} 

So, I know that there is IDS. However, when I print tag.id instead, I just get a "No" list. What am I doing wrong here?

+7
python html html-parsing beautifulsoup
source share
1 answer

You can access tag attributes by treating the tag as a dictionary ( documentation ):

 for tag in soup.find_all(class_="bookmark blurb group") : print tag.get('id') 

The reason tag.id didn't help is because it is equivalent to tag.find('id') , which results in None , since the id tag was not found ( documentation ).

+12
source share

All Articles