Use id=True
to match only elements that have the attribute set:
soup.find_all('div', id=True)
The inverse works too; you can exclude tags with the id
attribute:
soup.find_all('div', id=False):
To find tags with a given attribute you can also use CSS selectors:
soup.select('div[id]'):
but this does not support the operators needed to search for the inverse, unfortunately.
Demo:
>>> from bs4 import BeautifulSoup
>>> sample = '''\
... <div id="id1">This has an id</div>
... <div>This has none</div>
... <div id="id2">This one has an id too</div>
... <div>But this one has no clue (or id)</div>
... '''
>>> soup = BeautifulSoup(sample)
>>> soup.find_all('div', id=True)
[<div id="id1">This has an id</div>, <div id="id2">This one has an id too</div>]
>>> soup.find_all('div', id=False)
[<div>This has none</div>, <div>But this one has no clue (or id)</div>]
>>> soup.select('div[id]')
[<div id="id1">This has an id</div>, <div id="id2">This one has an id too</div>]