I want to call a Beautiful Soup attributes (eg. class_, href, id) from a variable to use it in functions such as this one:
script
from b
all credit to MYGz and commandlineluser
from bs4 import BeautifulSoup
data='<p class="story">xxx </p> <p id="2">yyy</p> <p class="story"> zzz</p>'
def removeAttrib(data, tag, kwargs):
soup = BeautifulSoup(data, "html.parser")
for x in soup.findAll(tag, kwargs):
for key in kwargs:
# print(key) #>>class
x.attrs.pop(key, None) # attrs: to access the actual dict
#del x[key] would work also but will throw a KeyError if no key
print(soup)
return soup
data=removeAttrib(data,"p",{"class":"story"})
You can pass a dictionary instead:
from bs4 import BeautifulSoup
data='<p class="story">xxx </p> <p id="2">yyy</p> <p class="story"> zzz</p>'
soup = BeautifulSoup(data, "html.parser")
def removeAttrib(soup, tag, argdict):
for x in soup.findAll(tag, argdict):
x.decompose()
removeAttrib(soup, "p", {"class": "story"})