Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find an element having an attribute of unknown value using BeautifulSoup?

This is how it is done when attribute value we are looking for is known:

from bs4 import BeautifulStoneSoup
soup = BeautifulStoneSoup(html, 'html.parser')
found_elems = soup.find_all(attrs={"myattribute" : "myknownvalue"})

How do I find all elements with "myattribute" attribute, not knowing it's value?

like image 427
ed22 Avatar asked Sep 11 '25 23:09

ed22


1 Answers

If you don't know value of attribute, set it to True:

from bs4 import BeautifulStoneSoup
soup = BeautifulStoneSoup(html, 'html.parser')
found_elems = soup.find_all(attrs={"myattribute": True})
like image 77
Vlad Havriuk Avatar answered Sep 14 '25 12:09

Vlad Havriuk