How can I find all elements with a custom html attribute using Beautiful Soup?
I have two cases where I want to clean up the html with custom html attributes This is a html example. How do you clear all elements with a custom "limit" attribute.
<div class="names" limit="10">Bar</div>
<div id="30" limit="20">Foo</div>
<li limit="x">Baz</li>
The second case is similar, but with all html tags
<div class="names" limit="10">Bar</div>
<div class="names" limit="20">Bar</div>
<div class="names" limit="30">Bar</div>
My question is different from How to find tags with only certain attributes - BeautifulSoup as the latter sets the values ββof attributes with a specific tag, whereas the first (my question) looks to assign an attribute only despite the tag or value
+3
source to share
1 answer
# First case:
soup.find_all(attrs={"limit":True})
# Second case:
soup.find_all("div", attrs={"limit":True})
Link:
- http://www.crummy.com/software/BeautifulSoup/bs4/doc/#kwargs
- http://www.crummy.com/software/BeautifulSoup/bs4/doc/#find-all
If your attribute name does not clash with either the Python keywords or the soup.find_all
args name, the syntax is simpler:
soup.find_all(id=True)
+14
source to share